結果

問題 No.861 ケーキカット
ユーザー kwm_tkwm_t
提出日時 2023-05-01 18:11:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 1,000 ms
コード長 1,936 bytes
コンパイル時間 4,791 ms
コンパイル使用メモリ 267,496 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-30 18:05:48
合計ジャッジ時間 6,175 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
6,812 KB
testcase_01 AC 61 ms
6,944 KB
testcase_02 AC 59 ms
6,944 KB
testcase_03 AC 58 ms
6,940 KB
testcase_04 AC 58 ms
6,940 KB
testcase_05 AC 60 ms
6,944 KB
testcase_06 AC 59 ms
6,944 KB
testcase_07 AC 59 ms
6,940 KB
testcase_08 AC 61 ms
6,944 KB
testcase_09 AC 61 ms
6,944 KB
testcase_10 AC 62 ms
6,940 KB
testcase_11 AC 61 ms
6,940 KB
testcase_12 AC 60 ms
6,940 KB
testcase_13 AC 60 ms
6,940 KB
testcase_14 AC 61 ms
6,940 KB
testcase_15 AC 58 ms
6,940 KB
testcase_16 AC 59 ms
6,940 KB
testcase_17 AC 61 ms
6,944 KB
testcase_18 AC 60 ms
6,940 KB
testcase_19 AC 59 ms
6,940 KB
testcase_20 AC 58 ms
6,940 KB
testcase_21 AC 58 ms
6,940 KB
testcase_22 AC 67 ms
6,944 KB
testcase_23 AC 68 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
int index(int x, int y) {
	return x * 5 + y;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	vector a(5, vector<int>(5));
	rep(i, 5)rep(j, 5)cin >> a[i][j];
	long long ans = LINF;
	vector<int>v0 = { 0,1,2,3,4,9,14,19,24,23,22,21,20,15,10,5 };
	vector<int>v1 = { 6,7,8,11,12,13,16,17,18 };
	rep(i, v0.size())rep2(j, i, v0.size()) {
		vector<int>c(25);
		rep2(k, i, j + 1) c[v0[k]] = 1;
		rep(k, 1 << 9) {
			auto cc = c;
			rep(l, 9) {
				if (1 & (k >> l))cc[v1[l]] = 1;
			}
			dsu uf(25);
			rep(s, 5) rep(t, 5) {
				if (s != 4) {
					int ns = s + 1;
					int nt = t + 0;
					int i0 = index(s, t);
					int i1 = index(ns, nt);
					if (cc[i0] == cc[i1])uf.merge(i0, i1);
				}
				if (t != 4) {
					int ns = s + 0;
					int nt = t + 1;
					int i0 = index(s, t);
					int i1 = index(ns, nt);
					if (cc[i0] == cc[i1])uf.merge(i0, i1);
				}
			}
			auto v = uf.groups();
			if (2 != v.size()) continue;
			long long val = 0;
			rep(l, 25) {
				if (cc[l])val += a[l / 5][l % 5];
				else val -= a[l / 5][l % 5];
			}
			chmin(ans, abs(val));
		}
	}
	cout << ans << endl;
	return 0;
}
0