結果

問題 No.861 ケーキカット
ユーザー nmnmnmnmnmnmnmnmnmnmnmnmnmnm
提出日時 2018-04-29 19:57:11
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 42 ms / 1,000 ms
コード長 2,860 bytes
コンパイル時間 1,165 ms
コンパイル使用メモリ 93,968 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 02:11:06
合計ジャッジ時間 3,859 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iomanip>
using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define per(i,a,b) for(ll i=(b-1);i>=(a);--i)
#define clr(a, b) memset((a), (b) ,sizeof(a))
#define ctos(c) string(1,c)
#define print(x) cout<<#x<<" = "<<x<<endl;

#define MOD 1000000007

ll d[5][5];
ll dd[16][2] = {
  {0, 0},
  {0, 1},
  {0, 2},
  {0, 3},
  {0, 4},
  {1, 4},
  {2, 4},
  {3, 4},
  {4, 4},
  {4, 3},
  {4, 2},
  {4, 1},
  {4, 0},
  {3, 0},
  {2, 0},
  {1, 0}
};
ll ddd[5][5];

vector<int> uf, usz;
int nc;

void init(int n) {
  vector<int> uf_(n);
  vector<int> usz_(n, 1);
  uf = uf_;
  usz = usz_;
  nc = n;

  for (int i = 0; i < uf.size(); i++) {
    uf[i] = i;
  }
}

int find(int a) {
  return (uf[a] == a) ? a : uf[a] = find(uf[a]);
}

void union_(int a, int b) {
  a = find(a);
  b = find(b);

  if (a != b) {
    if (usz[a] >= usz[b]) {
      swap(a, b);
    }
    uf[a] = b;
    usz[b] += usz[a];
    nc--;
  }
}

int check(int a, int b) {
  return (find(a) == find(b)) ? 1 : 0;
}

int get_size(int a) {
  return usz[find(a)];
}

int main() {
  ll mn = 1000000000000000000LL;
  rep(y, 0, 5) {
    rep(x, 0, 5) {
      ll a;
      cin >> a;
      d[y][x] = a;
    }
  }
  rep(i, 0, 16) {
    rep(j, i, 16) {
      rep(k, 0, 1 << 9) {
        clr(ddd, 0);
        rep(k, 0, 16) {
          if (i <= k && k <= j) {
            ddd[dd[k][0]][dd[k][1]] = 1;
          }
        }
        rep(l, 0, 9) {
          ll y = (l / 3) + 1;
          ll x = (l % 3) + 1;
          if (((k >> l) & 1) == 1) {
            ddd[y][x] = 1;
          }
        }
        init(25);
        rep(y, 0, 5) {
          rep(x, 0, 5) {
            ll index = 5 * y + x;
            if (y < 4) {
              if (ddd[y][x] == ddd[y + 1][x]) {
                union_(index, index + 5);
              }
            }
            if (x < 4) {
              if (ddd[y][x] == ddd[y][x + 1]) {
                union_(index, index + 1);
              }
            }
          }
        }
        if (nc == 2) {
          ll a = 0;
          ll b = 0;
          rep(y, 0, 5) {
            rep(x, 0, 5) {
              if (ddd[y][x] == 1) {
                a += d[y][x];
              }
              else {
                b += d[y][x];
              }
            }
          }
          mn = min(mn, abs(a - b));
        }
      }
    }
  }
  cout << mn << endl;
  return 0;
}
0