結果

問題 No.2696 Sign Creation
ユーザー rlangevinrlangevin
提出日時 2024-03-28 12:20:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 574 ms / 2,500 ms
コード長 2,732 bytes
コンパイル時間 290 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 82,180 KB
最終ジャッジ日時 2024-03-28 12:20:12
合計ジャッジ時間 8,060 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
59,848 KB
testcase_01 AC 44 ms
59,848 KB
testcase_02 AC 38 ms
53,460 KB
testcase_03 AC 41 ms
59,464 KB
testcase_04 AC 39 ms
53,460 KB
testcase_05 AC 44 ms
59,464 KB
testcase_06 AC 71 ms
53,460 KB
testcase_07 AC 37 ms
53,460 KB
testcase_08 AC 95 ms
76,212 KB
testcase_09 AC 56 ms
64,060 KB
testcase_10 AC 109 ms
76,368 KB
testcase_11 AC 62 ms
68,348 KB
testcase_12 AC 62 ms
68,348 KB
testcase_13 AC 362 ms
79,332 KB
testcase_14 AC 346 ms
79,980 KB
testcase_15 AC 299 ms
79,756 KB
testcase_16 AC 328 ms
79,140 KB
testcase_17 AC 128 ms
76,584 KB
testcase_18 AC 157 ms
77,740 KB
testcase_19 AC 322 ms
79,512 KB
testcase_20 AC 74 ms
70,432 KB
testcase_21 AC 295 ms
80,420 KB
testcase_22 AC 181 ms
77,760 KB
testcase_23 AC 292 ms
79,680 KB
testcase_24 AC 313 ms
79,772 KB
testcase_25 AC 272 ms
79,072 KB
testcase_26 AC 161 ms
77,048 KB
testcase_27 AC 191 ms
77,108 KB
testcase_28 AC 164 ms
77,036 KB
testcase_29 AC 132 ms
76,612 KB
testcase_30 AC 171 ms
76,732 KB
testcase_31 AC 574 ms
82,180 KB
testcase_32 AC 42 ms
59,464 KB
testcase_33 AC 50 ms
64,060 KB
testcase_34 AC 47 ms
61,836 KB
testcase_35 AC 37 ms
53,460 KB
testcase_36 AC 38 ms
53,460 KB
testcase_37 AC 38 ms
53,460 KB
testcase_38 AC 39 ms
53,460 KB
testcase_39 AC 39 ms
53,460 KB
testcase_40 AC 37 ms
53,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class UnionFind():
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.rank[x] < self.rank[y]:
                x, y = y, x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
            self.par[y] = x
            self.size[x] += self.size[y]

    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]


H, W, N, D = map(int, input().split())
X, Y = [0] * N, [0] * N
U = UnionFind(N)
SS = [[-1] * (W + 1) for _ in range(H + 1)]
for i in range(N):
    X[i], Y[i] = map(int, input().split())
    X[i], Y[i] = X[i] - 1, Y[i] - 1
    SS[X[i]][Y[i]] = i
    
L = []
for a in range(-D, D + 1):
    for b in range(-D, D + 1):
        if abs(a) + abs(b) > D:
            continue
        L.append((a, b))
    
for i in range(N):
    for dx, dy in L:
        a = X[i] + dx
        b = Y[i] + dy
        if a < 0 or a > H - 1 or b < 0 or b > W - 1:
            continue
        if SS[a][b] == -1:
            continue
        U.union(i, SS[a][b])
         
   
mi, ma = 10 ** 18, -10 ** 18
for i in range(H):
    for j in range(W):
        if SS[i][j] != -1:
            continue
        S = set()
        v1, v2 = 0, 0
        for dx, dy in L:
            a = i + dx
            b = j + dy
            if a < 0 or a > H - 1 or b < 0 or b > W - 1:
                continue
            if SS[a][b] == -1:
                continue
            ind = U.find(SS[a][b])
            if ind in S:
                continue
            S.add(ind)
            if U.get_size(ind) >= 2:
                v2 += 1
            else:
                v1 += 1
                    
        if v1 == 0:
            if v2 == 0:                         
                mi = min(mi, 0)
                ma = max(ma, 0)
            else:
                mi = min(mi, v2 - 1)
                ma = max(ma, v2 - 1)
        else:
            if v2 == 0:                         
                mi = min(mi, -1)
                ma = max(ma, -1)
            else:
                mi = min(mi, v2 - 1)
                ma = max(ma, v2 - 1)
        
ans = 0
S = set()
for i in range(N):
    if U.find(i) in S:
        continue
    S.add(U.find(i))
    if U.get_size(i) >= 2:
        ans += 1
        
print(ans - ma, ans - mi)
0