結果

問題 No.861 ケーキカット
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-08-14 03:29:10
言語 D
(dmd 2.106.1)
結果
WA  
実行時間 -
コード長 2,398 bytes
コンパイル時間 755 ms
コンパイル使用メモリ 107,928 KB
実行使用メモリ 168,620 KB
最終ジャッジ日時 2023-09-04 02:10:49
合計ジャッジ時間 11,660 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 388 ms
166,560 KB
testcase_01 AC 389 ms
166,584 KB
testcase_02 AC 391 ms
166,576 KB
testcase_03 AC 389 ms
166,592 KB
testcase_04 AC 390 ms
166,588 KB
testcase_05 AC 390 ms
166,628 KB
testcase_06 AC 392 ms
166,588 KB
testcase_07 AC 388 ms
166,628 KB
testcase_08 AC 391 ms
166,636 KB
testcase_09 AC 394 ms
166,700 KB
testcase_10 AC 401 ms
166,636 KB
testcase_11 AC 398 ms
166,632 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
権限があれば一括ダウンロードができます

ソースコード

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)); }


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]) {
          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