結果

問題 No.861 ケーキカット
ユーザー 👑 jupirojupiro
提出日時 2020-01-26 19:05:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 603 ms / 1,000 ms
コード長 2,845 bytes
コンパイル時間 2,385 ms
コンパイル使用メモリ 118,904 KB
実行使用メモリ 7,436 KB
最終ジャッジ日時 2023-09-22 02:26:34
合計ジャッジ時間 17,373 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 582 ms
7,196 KB
testcase_01 AC 589 ms
7,216 KB
testcase_02 AC 580 ms
7,172 KB
testcase_03 AC 584 ms
7,100 KB
testcase_04 AC 591 ms
7,432 KB
testcase_05 AC 587 ms
7,428 KB
testcase_06 AC 582 ms
7,164 KB
testcase_07 AC 582 ms
7,192 KB
testcase_08 AC 594 ms
7,288 KB
testcase_09 AC 584 ms
7,200 KB
testcase_10 AC 588 ms
7,204 KB
testcase_11 AC 589 ms
7,164 KB
testcase_12 AC 586 ms
7,196 KB
testcase_13 AC 590 ms
7,148 KB
testcase_14 AC 591 ms
7,168 KB
testcase_15 AC 588 ms
7,204 KB
testcase_16 AC 603 ms
6,112 KB
testcase_17 AC 597 ms
7,436 KB
testcase_18 AC 591 ms
7,240 KB
testcase_19 AC 589 ms
7,180 KB
testcase_20 AC 587 ms
7,200 KB
testcase_21 AC 596 ms
7,188 KB
testcase_22 AC 585 ms
7,272 KB
testcase_23 AC 584 ms
7,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long MAX = 5100000;
const long long INF = 1LL << 60;
const long long mod = 1000000007LL;
//const long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

class UnionFind {
public:
	vector<ll> Parent;
	UnionFind(ll N) {
		Parent = vector<ll>(N, -1);
	}
	ll root(ll A) {
		if (Parent[A] < 0) {
			return A;
		}
		return Parent[A] = root(Parent[A]);
	}

	ll size(ll A) {
		return -Parent[root(A)];
	}

	bool connect(ll A, ll B) {
		A = root(A);
		B = root(B);
		if (A == B) {
			return false;
		}
		if (size(A) < size(B)) {
			swap(A, B);
		}

		Parent[A] += Parent[B];
		Parent[B] = A;

		return true;
	}

};
ll c[5][5];
ll cnv(ll h, ll w) {
	return h * 5 + w;
}
ll res = INF;
ll all;
vector<bool> visited;
void dfs(ll S) {
	visited[S] = true;
	{
		UnionFind uni(25);
		ll tmp = 0;
		for (ll i = 0; i < 5; i++) {
			for (ll j = 0; j < 5; j++) {
				bool flag = (S >> cnv(i, j) & 1);
				if (flag) tmp += c[i][j];
				if (i > 0 && (flag == (S >> cnv(i - 1, j) & 1))) uni.connect(cnv(i, j), cnv(i - 1, j));
				//if (i < 4 && (flag == (S >> cnv(i + 1, j) & 1))) uni.connect(cnv(i, j), cnv(i + 1, j));
				if (j > 0 && (flag == (S >> cnv(i, j - 1) & 1))) uni.connect(cnv(i, j), cnv(i, j - 1));
				//if (j < 4 && (flag == (S >> cnv(i, j + 1) & 1))) uni.connect(cnv(i, j), cnv(i, j + 1));
			}
		}
        ll cnt[25] ={};
		for (ll i = 0; i < 25; i++) cnt[uni.root(i)]++;
      	ll t = 0; for(ll i=0;i<25;i++) t += (cnt[i] > 0);
		if(t <= 2) chmin(res, llabs(tmp - (all - tmp)));
	}
	for (ll i = 0; i < 5; i++) for (ll j = 0; j < 5; j++) if (!(S >> cnv(i, j) & 1)) {
		bool flag = false;
		if (i > 0 && (S >> cnv(i - 1, j)) & 1) flag = true;
		if (i < 4 && (S >> cnv(i + 1, j)) & 1) flag = true;
		if (j > 0 && (S >> cnv(i, j - 1) & 1)) flag = true;
		if (j < 4 && (S >> cnv(i, j + 1) & 1)) flag = true;
		ll ns = S + (1LL << cnv(i, j));
		if (flag && !visited[ns]) dfs(S + (1LL << cnv(i, j)));
	}
}
int main()
{
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/
	for (ll i = 0; i < 5; i++) for (ll j = 0; j < 5; j++) {
		scanf("%lld", &c[i][j]); all += c[i][j];
	}

	visited = vector<bool>(1LL << 25, false);
	dfs(1);

	cout << res << endl;
	return 0;
}
0