結果

問題 No.861 ケーキカット
ユーザー 👑 jupirojupiro
提出日時 2020-01-26 18:54:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 876 ms / 1,000 ms
コード長 2,841 bytes
コンパイル時間 1,123 ms
コンパイル使用メモリ 118,328 KB
実行使用メモリ 7,460 KB
最終ジャッジ日時 2023-09-22 02:20:30
合計ジャッジ時間 23,953 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 866 ms
7,184 KB
testcase_01 AC 876 ms
7,164 KB
testcase_02 AC 874 ms
7,200 KB
testcase_03 AC 862 ms
7,292 KB
testcase_04 AC 859 ms
7,252 KB
testcase_05 AC 860 ms
7,292 KB
testcase_06 AC 861 ms
7,244 KB
testcase_07 AC 868 ms
7,276 KB
testcase_08 AC 865 ms
7,244 KB
testcase_09 AC 866 ms
7,292 KB
testcase_10 AC 869 ms
7,460 KB
testcase_11 AC 865 ms
7,176 KB
testcase_12 AC 865 ms
7,436 KB
testcase_13 AC 863 ms
7,220 KB
testcase_14 AC 871 ms
7,184 KB
testcase_15 AC 871 ms
7,240 KB
testcase_16 AC 867 ms
7,452 KB
testcase_17 AC 870 ms
7,136 KB
testcase_18 AC 872 ms
7,204 KB
testcase_19 AC 874 ms
7,236 KB
testcase_20 AC 866 ms
7,164 KB
testcase_21 AC 864 ms
7,240 KB
testcase_22 AC 867 ms
7,200 KB
testcase_23 AC 866 ms
7,200 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