結果

問題 No.2696 Sign Creation
コンテスト
ユーザー kwm_t
提出日時 2026-10-18 20:36:24
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 55 ms / 2,500 ms
+ 756µs
コード長 2,558 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,005 ms
コンパイル使用メモリ 385,264 KB
実行使用メモリ 23,424 KB
最終ジャッジ日時 2026-10-18 20:36:34
合計ジャッジ時間 7,998 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#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 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; }
vector<P> createDlist(int d) {
	vector<P>ret;
	rep2(j, -d, d + 1)rep2(k, -d, d + 1) {
		if (abs(j) + abs(k) > d)continue;
		ret.push_back({ j,k });
	}
	return ret;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int h, w, n, d; cin >> h >> w >> n >> d;
	vector index(h, vector<int>(w, -1));
	vector<P>xy(n);
	auto ds = createDlist(d);
	rep(i, n) {
		cin >> xy[i].first >> xy[i].second;
		xy[i].first--, xy[i].second--;
		index[xy[i].first][xy[i].second] = i;
	}
	dsu uf(n);
	rep(i, n) {
		for (auto& [dx, dy] : ds) {
			P np = xy[i];
			int px = np.first + dx;
			int py = np.second + dy;
			if (px < 0 || px >= h)continue;
			if (py < 0 || py >= w)continue;
			int idx = index[px][py];
			if (-1 == idx)continue;
			uf.merge(i, idx);
		}
	}
	vector learder(h, vector<int>(w, -1));
	vector size(h, vector<int>(w, -1));
	int cnt = 0;
	auto gs = uf.groups();
	for (auto g : gs) {
		if (g.size() > 1)cnt++;
	}
	rep(i, h)rep(j, w)if (index[i][j] != -1) {
		learder[i][j] = uf.leader(index[i][j]);
		size[i][j] = uf.size(index[i][j]);
	}
	int mn = INF;
	int mx = 0;
	rep(i, h)rep(j, w)if (index[i][j] == -1) {
		set<int>st;
		int red = 0;
		int add = 0;
		for (auto& [dx, dy] : ds) {
			P np = xy[i];
			int px = i + dx;
			int py = j + dy;
			if (px < 0 || px >= h)continue;
			if (py < 0 || py >= w)continue;
			int idx = index[px][py];
			if (-1 == idx)continue;
			if (size[px][py] == 1) {
				add++;
			}
			else {
				int l = learder[px][py];
				if (st.count(l))continue;
				st.insert(l);
			}
		}
		if (st.empty() && add == 0) {
			chmin(mn, cnt);
			chmax(mx, cnt);
		}
		else {
			auto ccnt = cnt;
			ccnt -= st.size();
			ccnt++;
			chmin(mn, ccnt);
			chmax(mx, ccnt);
		}
	}
	cout << mn << " " << mx << endl;
	return 0;
}
0