結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 381 ms
166,684 KB
testcase_01 AC 403 ms
166,832 KB
testcase_02 AC 379 ms
166,620 KB
testcase_03 AC 378 ms
166,816 KB
testcase_04 AC 376 ms
166,752 KB
testcase_05 AC 375 ms
166,760 KB
testcase_06 AC 387 ms
166,748 KB
testcase_07 AC 377 ms
166,576 KB
testcase_08 AC 374 ms
166,572 KB
testcase_09 AC 370 ms
166,624 KB
testcase_10 AC 379 ms
166,576 KB
testcase_11 AC 380 ms
166,616 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;
          foreach (u; 0 .. V) {
            diff += (((p >> u) & 1) ? -1 : +1) * C[u];
          }
          chmin(ans, abs(diff));
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0