結果

問題 No.2696 Sign Creation
ユーザー detteiuudetteiuu
提出日時 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
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
53,248 KB
testcase_01 AC 42 ms
53,248 KB
testcase_02 AC 41 ms
52,992 KB
testcase_03 AC 42 ms
53,120 KB
testcase_04 AC 44 ms
53,760 KB
testcase_05 AC 43 ms
53,504 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 141 ms
77,952 KB
testcase_09 WA -
testcase_10 AC 201 ms
79,292 KB
testcase_11 AC 87 ms
76,288 KB
testcase_12 AC 93 ms
76,160 KB
testcase_13 AC 1,086 ms
87,296 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 935 ms
87,168 KB
testcase_17 AC 153 ms
77,056 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 127 ms
77,056 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 295 ms
79,068 KB
testcase_28 WA -
testcase_29 AC 278 ms
77,608 KB
testcase_30 WA -
testcase_31 AC 1,960 ms
114,816 KB
testcase_32 AC 39 ms
52,992 KB
testcase_33 AC 53 ms
65,152 KB
testcase_34 AC 49 ms
61,696 KB
testcase_35 AC 39 ms
52,992 KB
testcase_36 AC 38 ms
52,992 KB
testcase_37 AC 39 ms
52,608 KB
testcase_38 AC 37 ms
52,992 KB
testcase_39 WA -
testcase_40 WA -
権限があれば一括ダウンロードができます

ソースコード

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