結果

問題 No.861 ケーキカット
ユーザー pekempeypekempey
提出日時 2019-08-09 23:02:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,672 bytes
コンパイル時間 1,582 ms
コンパイル使用メモリ 170,308 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-26 21:40:58
合計ジャッジ時間 7,333 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
権限があれば一括ダウンロードができます

ソースコード

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 st = 0;
      rep(i, (k+1)*5) {
        if (uf2.find(i) < k * 5) 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