結果

問題 No.2731 Two Colors
ユーザー binapbinap
提出日時 2024-04-19 23:08:26
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 449 ms / 3,000 ms
コード長 2,323 bytes
コンパイル時間 4,824 ms
コンパイル使用メモリ 258,568 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-04-19 23:08:57
合計ジャッジ時間 11,387 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 449 ms
11,392 KB
testcase_01 AC 427 ms
11,520 KB
testcase_02 AC 429 ms
11,520 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 23 ms
5,376 KB
testcase_05 AC 13 ms
5,376 KB
testcase_06 AC 55 ms
5,376 KB
testcase_07 AC 73 ms
5,376 KB
testcase_08 AC 10 ms
5,376 KB
testcase_09 AC 250 ms
9,056 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 223 ms
10,092 KB
testcase_12 AC 259 ms
9,164 KB
testcase_13 AC 187 ms
8,368 KB
testcase_14 AC 107 ms
6,144 KB
testcase_15 AC 9 ms
5,376 KB
testcase_16 AC 94 ms
5,672 KB
testcase_17 AC 137 ms
6,736 KB
testcase_18 AC 23 ms
5,376 KB
testcase_19 AC 97 ms
5,724 KB
testcase_20 AC 170 ms
7,168 KB
testcase_21 AC 25 ms
5,376 KB
testcase_22 AC 260 ms
9,488 KB
testcase_23 AC 61 ms
5,376 KB
testcase_24 AC 32 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 206 ms
8,040 KB
testcase_27 AC 265 ms
9,700 KB
testcase_28 AC 157 ms
7,528 KB
testcase_29 AC 47 ms
5,376 KB
testcase_30 AC 90 ms
5,888 KB
testcase_31 AC 51 ms
5,376 KB
testcase_32 AC 323 ms
10,648 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef long double ld;
typedef pair<int, int> P;

ostream& operator<<(ostream& os, const modint& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const static_modint<m>& a) {os << a.val(); return os;}
template <int m> ostream& operator<<(ostream& os, const dynamic_modint<m>& a) {os << a.val(); return os;}
template<typename T> istream& operator>>(istream& is, vector<T>& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;}
template<typename U, typename T> ostream& operator<<(ostream& os, const pair<U, T>& p){os << p.first << ' ' << p.second; return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;}

template<typename T> void chmin(T& a, T b){a = min(a, b);}
template<typename T> void chmax(T& a, T b){a = max(a, b);}

int main(){
	int h, w;
	cin >> h >> w;
	vector<vector<int>> a(h, vector<int>(w));
	cin >> a;
	using PQ = priority_queue<P, vector<P>, greater<P>>;
	PQ pq0;
	PQ pq1;
	pq0.emplace(0, 0);
	pq1.emplace(0, h * w - 1);
	int ans = -2;
	bool stop_flag = false;
	vector<int> dy = {1, 0, -1, 0};
	vector<int> dx = {0, 1, 0, -1};
	vector<vector<int>> col(h, vector<int>(w, -1));
	auto paint = [&](PQ& pq, int c){
		int tile = -1;
		int fy = -1;
		int fx = -1;
		while(true){
			auto p = pq.top(); pq.pop();
			tile = p.second;
			fy = tile / w;
			fx = tile % w;
			if(col[fy][fx] == -1) break;
		}
		col[fy][fx] = c;
		rep(t, 4){
			int ty = fy + dy[t];
			int tx = fx + dx[t];
			if(ty >= 0 and ty < h and tx >= 0 and tx < w){
				if(col[ty][tx] == -1){
					pq.emplace(a[ty][tx], ty * w + tx);
				}else{
					if(col[ty][tx] != c) stop_flag = true;
				}
			}
		}
		ans++;
	};
	while(true){
		paint(pq0, 0);
		if(stop_flag) break;
		paint(pq1, 1);
		if(stop_flag) break;
	}
	cout << ans << "\n";
	return 0;
}
0