結果

問題 No.861 ケーキカット
ユーザー pekempeypekempey
提出日時 2019-08-09 23:06:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 292 ms / 1,000 ms
コード長 1,814 bytes
コンパイル時間 1,677 ms
コンパイル使用メモリ 171,488 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 02:14:58
合計ジャッジ時間 10,063 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 286 ms
4,376 KB
testcase_01 AC 284 ms
4,380 KB
testcase_02 AC 284 ms
4,380 KB
testcase_03 AC 285 ms
4,380 KB
testcase_04 AC 284 ms
4,380 KB
testcase_05 AC 283 ms
4,376 KB
testcase_06 AC 292 ms
4,380 KB
testcase_07 AC 284 ms
4,380 KB
testcase_08 AC 285 ms
4,380 KB
testcase_09 AC 287 ms
4,380 KB
testcase_10 AC 284 ms
4,376 KB
testcase_11 AC 284 ms
4,376 KB
testcase_12 AC 285 ms
4,376 KB
testcase_13 AC 284 ms
4,376 KB
testcase_14 AC 283 ms
4,380 KB
testcase_15 AC 286 ms
4,380 KB
testcase_16 AC 286 ms
4,376 KB
testcase_17 AC 285 ms
4,376 KB
testcase_18 AC 285 ms
4,380 KB
testcase_19 AC 286 ms
4,380 KB
testcase_20 AC 289 ms
4,380 KB
testcase_21 AC 285 ms
4,376 KB
testcase_22 AC 287 ms
4,376 KB
testcase_23 AC 284 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)

using namespace std;
using ll = long long;

struct unionfind {
  vector<int> dat;

  unionfind(int n) : dat(n) {
    rep(i, n) dat[i] = i;
  }

  int find(int x) {
    if (x == dat[x]) return x;
    return dat[x] = find(dat[x]);
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    dat[x] = dat[y] = max(x, y);
  }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll C[5][5];
  rep(i, 5) rep(j, 5) cin >> C[i][j];
  int color[5][5] = {};
  ll ans = 1e18;
  auto dfs = [&](auto dfs, int k, unionfind uf) -> void {
    if (k == 5) {
      int st = 0;
      rep(i, 25) st |= 1 << uf.find(i);
      if (__builtin_popcount(st) != 2) return;
      ll x = 0;
      /*
      rep(i, 5) {
        rep(j, 5) {
          cout << color[i][j];
        }
        cout << endl;
      }
      cout << endl;
      */
      rep(i, 5) rep(j, 5) {
        if (color[i][j]) x += C[i][j];
        else x -= C[i][j];
      }
      ans = min(ans, abs(x));
      return;
    }
    rep(x, 1 << 5) {
      unionfind uf2 = uf;
      rep(j, 5) color[k][j] = x >> j & 1;
      rep(j, 4) {
        if (color[k][j] == color[k][j + 1]) uf2.unite(k*5+j, k*5+j+1);
      }
      if (k > 0) {
        rep(j, 5) if (color[k][j] == color[k-1][j]) uf2.unite(k*5+j, k*5+j-5);
      }
      int xs = 0;
      rep(j, 5) xs |= 1 << color[k][j];
      int st = 0;
      rep(i, (k+1)*5) {
        if (uf2.find(i) < k * 5) {
          if ((1 << color[i / 5][i % 5] & xs) != 0) st = 0x7;
          st |= 1 << uf2.find(i);
        }
      }
      if (__builtin_popcount(st) < 2) {
        dfs(dfs, k + 1, uf2);
      }
    }
  };
  unionfind uf(25);
  dfs(dfs, 0, uf);
  cout << ans << endl;
}
0