結果

問題 No.2696 Sign Creation
ユーザー detteiuu
提出日時 2024-08-10 16:08:22
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,450 bytes
コンパイル時間 296 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 114,816 KB
最終ジャッジ日時 2024-08-10 16:08:38
合計ジャッジ時間 15,419 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21 WA * 17
権限があれば一括ダウンロードができます

ソースコード

diff #

from bisect import bisect_left, bisect_right

class UnionFind:
    def __init__(self,n):
        self.n = n
        self.parent_size = [-1]*n
 
    def leader(self,a):
        if self.parent_size[a] < 0:
            return a
        self.parent_size[a] = self.leader(self.parent_size[a])
        return self.parent_size[a]
 
    def merge(self,a,b):
        x, y = self.leader(a), self.leader(b)
        if x == y:
            return 
        if abs(self.parent_size[x]) < abs(self.parent_size[y]):
            x, y = y, x
        self.parent_size[x] += self.parent_size[y]
        self.parent_size[y] = x
        return 
 
    def same(self,a,b):
        return self.leader(a) == self.leader(b)
 
    def size(self,a):
        return abs(self.parent_size[self.leader(a)])
 
    def groups(self):
        result = [[] for _ in range(self.n)]
        for i in range(self.n):
            result[self.leader(i)].append(i)
        return [r for r in result if r != []]
    
H, W, N, D = map(int, input().split())
XY = [list(map(int, input().split())) for _ in range(N)]

idx = dict()
xyD = dict()
for i in range(N):
    X, Y = XY[i]
    idx[(X, Y)] = i
    if X in xyD:
        xyD[X].append(Y)
    else:
        xyD[X] = [Y]
for i in xyD.keys():
    xyD[i].sort()

UF = UnionFind(N)
for i in range(N):
    X, Y = XY[i]
    for dx in range(D+1):
        for dy in range(-D, D+1):
            nx, ny = X+dx, Y+dy
            if (nx, ny) in idx:
                UF.merge(i, idx[(nx, ny)])

def func(x, y):
    S = set()
    for dx in range(-D, D+1):
        nx = x+dx
        if nx not in xyD:
            continue
        left = y-(D-abs(dx))
        right = y+(D-abs(dx))
        lb = bisect_left(xyD[nx], left)
        rb = bisect_right(xyD[nx], right)
        for i in range(lb, rb):
            S.add(UF.leader(idx[(nx, xyD[nx][i])]))
    if len(S) == 0:
        return L
    else:
        cnt = dict()
        for s in S:
            if UF.size(s) in cnt:
                cnt[UF.size(s)] += 1
            else:
                cnt[UF.size(s)] = 1
        if 1 in cnt:
            cnt.pop(1)
        c = sum(i for i in cnt.values())
        return L+1-c

INF = 10**18
ansMIN = INF
ansMAX = -INF
L = sum(len(g) >= 2 for g in UF.groups())
for x in range(1, H+1):
    for y in range(1, W+1):
        if (x, y) in idx:
            continue
        cnt = func(x, y)
        ansMIN = min(ansMIN, cnt)
        ansMAX = max(ansMAX, cnt)

print(ansMIN, ansMAX)
0