結果
| 問題 | No.2696 Sign Creation | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2024-03-22 22:55:36 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 541 ms / 2,500 ms | 
| コード長 | 2,507 bytes | 
| コンパイル時間 | 240 ms | 
| コンパイル使用メモリ | 82,380 KB | 
| 実行使用メモリ | 82,856 KB | 
| 最終ジャッジ日時 | 2024-12-20 12:20:38 | 
| 合計ジャッジ時間 | 9,394 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 38 | 
ソースコード
import sys
sys.setrecursionlimit(5*10**5)
input = sys.stdin.readline
from collections import defaultdict, deque, Counter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right
from math import gcd
from collections import defaultdict
class UnionFind:
    def __init__(self, n):
        self.n = n
        self.p = [-1] * (n+1)
    def find(self, x):
        if self.p[x] < 0:
            return x
        else:
            self.p[x] = self.find(self.p[x])
            return self.p[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.p[x] > self.p[y]:
            x, y = y, x
        self.p[x] += self.p[y]
        self.p[y] = x
    def same(self, a, b):
        return self.find(a) == self.find(b)
    def group(self):
        d = defaultdict(list)
        for i in range(1, self.n+1):
            par = self.find(i)
            d[par].append(i)
        return d
h,w,n,d = map(int,input().split())
g = [[-1]*w for i in range(h)]
now = 1
for i in range(n):
    n,m = map(int,input().split())
    g[n-1][m-1] = i
uf = UnionFind(h*w+1)
for i in range(h):
    for j in range(w):
        if g[i][j] == -1: continue
        for x in range(i-d,i+d+1):
            for y in range(j-d, j+d+1):
                if not (0<=x<h and 0<=y<w): continue
                if g[x][y] == -1: continue
                if(abs(x-i) + abs(y-j)) > d:continue
                uf.union(x*w+y, i*w+j)
cnt = [0]*(h*w)
s = set()
for i in range(h):
    for j in range(w):
        if g[i][j] == -1: continue
        g[i][j] = uf.find(i*w+j)
        s.add(uf.find(i*w+j))
        cnt[uf.find(i*w+j)] += 1
tot = len(s) - cnt.count(1)
mn = tot
mx = tot
for i in range(h):
    for j in range(w):
        if g[i][j] != -1: continue
        two = 0
        one = 0
        wa = set()
        for x in range(i-d,i+d+1):
            for y in range(j-d, j+d+1):
                if not (0<=x<h and 0<=y<w): continue
                if g[x][y] == -1: continue
                if g[x][y] in wa: continue
                if(abs(x-i) + abs(y-j)) > d:continue
                wa.add(g[x][y])
                if cnt[g[x][y]] == 1:
                    one += 1
                else:
                    two += 1
        tmp = tot
        if two >= 1:
            tmp -= two-1
        else:
            if one:
                tmp += 1                    
        mn = min(tmp, mn)
        mx = max(tmp, mx)
print(mn,mx)
                
            
            
            
        