結果

問題 No.2696 Sign Creation
ユーザー shobonvip
提出日時 2024-03-23 04:27:59
言語 C++17(gcc12)
(gcc 12.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,492 ms / 2,500 ms
コード長 3,185 bytes
コンパイル時間 18,647 ms
コンパイル使用メモリ 327,528 KB
最終ジャッジ日時 2025-02-20 13:02:27
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

struct UndoUnionFind{
	int n;
	vector<int> par;
	vector<pair<int,int>> history;

	explicit UndoUnionFind(int v): n(v), par(v,-1) {}

	void merge(int a, int b){
		int x = find(a);
		int y = find(b);
		history.push_back(pair(x, par[x]));
		history.push_back(pair(y, par[y]));
		if (x == y) return;
		if (-par[x] < -par[y]) swap(x, y);
		par[x] += par[y];
		par[y] = x;
		return;
	}

	int find(int x){
		if (par[x] < 0) return x;
		return find(par[x]);
	}

	void undo(){
		par[history.back().first] = history.back().second;
		history.pop_back();
		par[history.back().first] = history.back().second;
		history.pop_back();
		return;
	};
};

int main(){
	int h, w; cin >> h >> w;
	int n, d; cin >> n >> d;

	vector<int> x(n), y(n);
	rep(i,0,n) cin >> x[i] >> y[i];

	map<pair<int,int>,int> taio;
	rep(i,0,n){
		taio[pair(x[i], y[i])] = i;
	}

	UndoUnionFind uf(n+1);
	int val = 0;
	rep(i,0,n){
		rep(dx,-d,d+1){
			rep(dy,-d,d+1){
				if (abs(dx)+abs(dy)>d) continue;
				if (taio.find(pair(x[i]+dx,y[i]+dy)) == taio.end()){
					continue;
				}
				if (uf.find(taio[pair(x[i]+dx,y[i]+dy)]) != uf.find(i)){
					if (-uf.par[uf.find(taio[pair(x[i]+dx,y[i]+dy)])] >= 2){
						val--;
					}
					if (-uf.par[uf.find(i)] >= 2){
						val--;
					}
					uf.merge(taio[pair(x[i]+dx,y[i]+dy)], i);
					if(-uf.par[uf.find(i)]>=2){
						val++;
					}
				}
			}
		}
	}

	int mn = 1e9;
	int mx = -1;

	rep(i,1,h+1){
		rep(j,1,w+1){
			if (taio.find(pair(i,j)) != taio.end()){
				continue;
			}
			int cnt = 0;
			int orval = val;
			rep(dx,-d,d+1){
				rep(dy,-d,d+1){
					if (abs(dx)+abs(dy) > d) continue;
					if (taio.find(pair(i+dx, j+dy)) == taio.end()){
						continue;
					}
					if (uf.find(taio[pair(i+dx, j+dy)]) != uf.find(n)){
						if (-uf.par[uf.find(taio[pair(i+dx, j+dy)])] >= 2){
							val--;
						}
						if( -uf.par[uf.find(n)] >= 2){
							val--;
						}
						uf.merge(taio[pair(i+dx, j+dy)], n);
						
						if( -uf.par[uf.find(n)] >= 2){
							val++;
						}
						cnt++;
					}
				}
			}


			chmax(mx,val);
			chmin(mn,val);

			rep(num,0,cnt){
				uf.undo();
			}
			val = orval;
		}
	}
	cout << mn << ' ' << mx << '\n';
}

0