結果

問題 No.861 ケーキカット
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-08-14 03:33:40
言語 D
(dmd 2.106.1)
結果
TLE  
実行時間 -
コード長 2,699 bytes
コンパイル時間 5,052 ms
コンパイル使用メモリ 112,296 KB
実行使用メモリ 168,732 KB
最終ジャッジ日時 2023-09-04 02:11:44
合計ジャッジ時間 28,813 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 919 ms
168,212 KB
testcase_01 AC 917 ms
167,364 KB
testcase_02 AC 918 ms
167,704 KB
testcase_03 AC 917 ms
166,968 KB
testcase_04 AC 918 ms
168,276 KB
testcase_05 AC 947 ms
167,936 KB
testcase_06 AC 974 ms
168,216 KB
testcase_07 AC 959 ms
168,172 KB
testcase_08 AC 932 ms
167,304 KB
testcase_09 AC 934 ms
168,348 KB
testcase_10 AC 924 ms
168,324 KB
testcase_11 AC 926 ms
168,660 KB
testcase_12 AC 926 ms
168,348 KB
testcase_13 TLE -
testcase_14 AC 966 ms
168,732 KB
testcase_15 AC 908 ms
168,488 KB
testcase_16 AC 940 ms
166,904 KB
testcase_17 AC 922 ms
168,688 KB
testcase_18 AC 916 ms
166,888 KB
testcase_19 AC 922 ms
167,216 KB
testcase_20 AC 960 ms
167,908 KB
testcase_21 AC 960 ms
168,552 KB
testcase_22 AC 948 ms
167,804 KB
testcase_23 AC 966 ms
168,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.complex, std.container, std.math, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


int root(int[] uf, int u) {
  return (uf[u] < 0) ? u : (uf[u] = uf.root(uf[u]));
}
bool connect(int[] uf, int u, int v) {
  u = uf.root(u);
  v = uf.root(v);
  if (u == v) return false;
  if (uf[u] > uf[v]) swap(u, v);
  uf[u] += uf[v];
  uf[v] = u;
  return true;
}

enum N = 5;
enum V = N * N;

int[] G, GSum;
bool[] vis;

void dfs(int p) {
  if (!vis[p]) {
    vis[p] = true;
    // if (2 * popcnt(p) < V) {
      foreach (v; 0 .. V) {
        if ((GSum[p] >> v) & 1) {
          dfs(p | 1 << v);
        }
      }
    // }
  }
}

enum INF = 10L^^18;

void main() {
  G = new int[V];
  foreach (u; 0 .. V) foreach (v; 0 .. V) {
    if (abs(u / N - v / N) + abs(u % N - v % N) == 1) {
      G[u] |= 1 << v;
    }
  }
  GSum = new int[1 << V];
  foreach (u; 0 .. V) foreach (p; 0 .. 1 << u) {
    GSum[p | 1 << u] = GSum[p] | G[u];
  }
  vis = new bool[1 << V];
  foreach (u; 0 .. V) {
    dfs(1 << u);
  }
  stderr.writeln(vis.count(true));
  
  try {
    for (; ; ) {
      auto C = new long[V];
      foreach (u; 0 .. V) {
        C[u] = readLong();
      }
      
      long ans = INF;
      foreach (p; 0 .. 1 << V) {
        if (vis[p] && vis[((1 << V) - 1) ^ p]) {
          long diff = 0;
          foreach (u; 0 .. V) {
            // dmd BUG
            // diff += (((p >> u) & 1) ? -1 : +1) * C[u];
            if ((p >> u) & 1) {
              diff -= C[u];
            } else {
              diff += C[u];
            }
          }
          chmin(ans, abs(diff));
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0