結果

問題 No.2696 Sign Creation
ユーザー ゆにぽけゆにぽけ
提出日時 2024-03-22 22:10:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,247 bytes
コンパイル時間 3,492 ms
コンパイル使用メモリ 139,052 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-22 22:10:20
合計ジャッジ時間 4,598 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 4 ms
6,676 KB
testcase_09 WA -
testcase_10 AC 4 ms
6,676 KB
testcase_11 AC 4 ms
6,676 KB
testcase_12 AC 4 ms
6,676 KB
testcase_13 AC 199 ms
6,676 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 142 ms
6,676 KB
testcase_17 AC 46 ms
6,676 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 6 ms
6,676 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 56 ms
6,676 KB
testcase_28 WA -
testcase_29 AC 50 ms
6,676 KB
testcase_30 WA -
testcase_31 AC 161 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 2 ms
6,676 KB
testcase_37 AC 2 ms
6,676 KB
testcase_38 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <iterator>
#include <string>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <ctime>
#include <iomanip>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <bitset>
#include <random>
#include <utility>
#include <functional>
using namespace std;

#include<algorithm>
#include<vector>
#include<cassert>
using namespace std;
struct UnionFind
{
	private:
	int n;
	vector<int> par,siz;

	public:
	UnionFind(int n) :n(n),par(n,-1),siz(n,1) {}

	int root(int u) 
	{
		assert(0 <= u && u < n);
		return (par[u] < 0 ? u:par[u] = root(par[u]));
	}

	bool same(int u,int v)
	{
		assert(0 <= u && u < n && 0 <= v && v < n);
		return root(u) == root(v);
	}

	bool unite(int u,int v)
	{
		assert(0 <= u && u < n && 0 <= v && v < n);
		u = root(u),v = root(v);
		if(u == v) return false;

		if(siz[u] < siz[v]) swap(u,v);

		siz[u] += siz[v];
		par[v] = u;

		return true;
	}

	int size(int u)
	{
		assert(0 <= u && u < n);
		return siz[root(u)];
	}

	vector<vector<int>> components()
	{
		vector<vector<int>> ret(n);
		for(int u = 0;u < n;u++) ret[root(u)].push_back(u);

		ret.erase(remove_if(ret.begin(),ret.end(),[](vector<int> v) { return v.empty();}),ret.end());

		return ret;
	}
};

void Main()
{
	int H,W,N,D;
	cin >> H >> W >> N >> D;
	vector<vector<int>> A(H,vector<int>(W,-1));
	for(int i = 0;i < N;i++)
	{
		int x,y;
		cin >> x >> y;
		x--; y--;
		A[x][y] = i;
	}
	UnionFind uf(N);
	for(int i = 0;i < H;i++)
	{
		for(int j = 0;j < W;j++)
		{
			if(A[i][j] == -1)
			{
				continue;
			}
			for(int ni = i - D;ni <= i + D;ni++)
			{
				for(int nj = j - D;nj <= j + D;nj++)
				{
					if(ni < 0 || nj < 0 || ni >= H || nj >= W || A[ni][nj] == -1)
					{
						continue;
					}
					uf.unite(A[i][j],A[ni][nj]);
				}
			}
		}
	}
	int cnt = 0;
	for(int i = 0;i < N;i++)
	{
		if(i == uf.root(i) && uf.size(i) >= 2)
		{
			cnt++;
		}
	}
	int mn = (int)1e9,mx = 0;
	vector<int> seen(N);
	for(int i = 0;i < H;i++)
	{
		for(int j = 0;j < W;j++)
		{
			if(A[i][j] != -1)
			{
				continue;
			}
			int c[2]{};
			for(int ni = i - D;ni <= i + D;ni++)
			{
				for(int nj = j - D;nj <= j + D;nj++)
				{
					if(ni < 0 || nj < 0 || ni >= H || nj >= W || A[ni][nj] == -1)
					{
						continue;
					}
					int id = uf.root(A[ni][nj]);
					if(!seen[id])
					{
						seen[id] = 1;
						if(uf.size(id) == 1)
						{
							c[0]++;
						}
						else
						{
							c[1]++;
						}
					}
				}
			}
			int cur = cnt;
			if(c[0] == 0)
			{
				if(c[1] != 0)
				{
					cur -= c[1] - 1;
				}
			}
			else
			{
				cur++;
				if(c[1] != 0)
				{
					cur -= c[1] - 1;
				}
			}
			mn = min(mn,cur);
			mx = max(mx,cur);
			for(int ni = i - D;ni <= i + D;ni++)
			{
				for(int nj = j - D;nj <= j + D;nj++)
				{
					if(ni < 0 || nj < 0 || ni >= H || nj >= W || A[ni][nj] == -1)
					{
						continue;
					}
					int id = uf.root(A[ni][nj]);
					seen[id] = 0;
				}
			}
		}
	}
	cout << mn << " " << mx << "\n";
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt = 1;
	/* cin >> tt; */
	while(tt--) Main();
}
0