結果

問題 No.2696 Sign Creation
ユーザー ntudantuda
提出日時 2024-03-23 20:41:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 782 ms / 2,500 ms
コード長 1,594 bytes
コンパイル時間 147 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 89,600 KB
最終ジャッジ日時 2024-03-27 18:00:45
合計ジャッジ時間 8,675 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
59,200 KB
testcase_01 AC 41 ms
59,200 KB
testcase_02 AC 38 ms
53,460 KB
testcase_03 AC 40 ms
59,200 KB
testcase_04 AC 38 ms
53,460 KB
testcase_05 AC 37 ms
53,460 KB
testcase_06 AC 37 ms
53,460 KB
testcase_07 AC 37 ms
53,460 KB
testcase_08 AC 133 ms
76,632 KB
testcase_09 AC 65 ms
70,224 KB
testcase_10 AC 158 ms
77,684 KB
testcase_11 AC 73 ms
75,548 KB
testcase_12 AC 72 ms
75,548 KB
testcase_13 AC 512 ms
82,404 KB
testcase_14 AC 378 ms
82,496 KB
testcase_15 AC 365 ms
82,484 KB
testcase_16 AC 467 ms
81,640 KB
testcase_17 AC 209 ms
76,800 KB
testcase_18 AC 242 ms
78,680 KB
testcase_19 AC 395 ms
81,252 KB
testcase_20 AC 83 ms
75,652 KB
testcase_21 AC 365 ms
83,796 KB
testcase_22 AC 288 ms
79,388 KB
testcase_23 AC 375 ms
83,016 KB
testcase_24 AC 378 ms
82,000 KB
testcase_25 AC 428 ms
81,320 KB
testcase_26 AC 254 ms
77,572 KB
testcase_27 AC 210 ms
77,976 KB
testcase_28 AC 211 ms
78,076 KB
testcase_29 AC 156 ms
76,688 KB
testcase_30 AC 228 ms
77,196 KB
testcase_31 AC 782 ms
89,600 KB
testcase_32 AC 40 ms
59,200 KB
testcase_33 AC 57 ms
70,524 KB
testcase_34 AC 42 ms
59,180 KB
testcase_35 AC 36 ms
53,460 KB
testcase_36 AC 36 ms
53,460 KB
testcase_37 AC 35 ms
53,460 KB
testcase_38 AC 35 ms
53,460 KB
testcase_39 AC 36 ms
53,460 KB
testcase_40 AC 37 ms
53,460 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def root(x):
    if P[x] < 0: return x
    P[x] = root(P[x])  # 経路圧縮
    return P[x]
def unite(x,y):
    x = root(x)
    y = root(y)
    if x == y: return
    if x > y: x,y = y,x
    P[x] += P[y]
    P[y] = x
def same(x,y):
    return root(x) == root(y)
def size(x):
    x = root(x)
    return -P[x]

H, W, N, D = map(int, input().split())
XY = [list(map(int,input().split())) for _ in range(N)]
P = [-1] * N
M = [[-1] * (W+1) for _ in range(H+1)]

for i,(x,y) in enumerate(XY):
    M[x][y] = i

def search(x, y):
    ret = set()
    for i in range(-D, D+1):
        if 0 < x + i <= H:
            xi = x + i
            for j in range(-D + abs(i), D - abs(i) + 1):
                if 0 < y + j <= W:
                    yj = y+j
                    if M[xi][yj] > -1:
                        ret.add(M[xi][yj])
    return ret

for i,(x,y) in enumerate(XY):
    for j in search(x,y):
        unite(i,j)

Q = set()
for i in range(N):
    if size(i) >= 2:
        Q.add(root(i))
gcount = len(Q)

ansmax = 0
ansmin = gcount
for i in range(1,H+1):
    for j in range(1,W+1):
        if M[i][j] > -1:
            continue
        tmp = search(i,j)
        Q1 = set()
        Q2 = set()
        for k in tmp:
            if size(k) > 1:
                Q2.add(root(k))
            else:
                Q1.add(k)
        if Q2:
            tmp2 = gcount - len(Q2) + 1
            ansmax = max(ansmax, tmp2)
            ansmin = min(ansmin, tmp2)
        elif Q1:
            tmp2 = gcount + 1
            ansmax = max(ansmax,tmp2)
            ansmin = min(ansmin,tmp2)

print(ansmin,ansmax)

0