結果

問題 No.2696 Sign Creation
ユーザー mo124121mo124121
提出日時 2024-02-04 10:38:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,302 ms / 2,500 ms
コード長 2,930 bytes
コンパイル時間 230 ms
コンパイル使用メモリ 82,600 KB
実行使用メモリ 96,160 KB
最終ジャッジ日時 2024-05-17 18:09:54
合計ジャッジ時間 11,585 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
63,512 KB
testcase_01 AC 50 ms
63,076 KB
testcase_02 AC 43 ms
55,744 KB
testcase_03 AC 51 ms
61,236 KB
testcase_04 AC 44 ms
56,852 KB
testcase_05 AC 47 ms
61,048 KB
testcase_06 AC 43 ms
55,384 KB
testcase_07 AC 42 ms
54,936 KB
testcase_08 AC 150 ms
78,196 KB
testcase_09 AC 69 ms
71,508 KB
testcase_10 AC 149 ms
77,936 KB
testcase_11 AC 93 ms
76,796 KB
testcase_12 AC 94 ms
76,548 KB
testcase_13 AC 723 ms
83,540 KB
testcase_14 AC 490 ms
83,308 KB
testcase_15 AC 444 ms
83,872 KB
testcase_16 AC 644 ms
83,588 KB
testcase_17 AC 293 ms
77,596 KB
testcase_18 AC 268 ms
79,092 KB
testcase_19 AC 458 ms
81,472 KB
testcase_20 AC 123 ms
76,804 KB
testcase_21 AC 357 ms
84,732 KB
testcase_22 AC 429 ms
78,544 KB
testcase_23 AC 448 ms
83,192 KB
testcase_24 AC 431 ms
82,688 KB
testcase_25 AC 524 ms
80,948 KB
testcase_26 AC 382 ms
77,864 KB
testcase_27 AC 370 ms
78,136 KB
testcase_28 AC 297 ms
78,584 KB
testcase_29 AC 352 ms
76,848 KB
testcase_30 AC 313 ms
77,704 KB
testcase_31 AC 1,302 ms
96,160 KB
testcase_32 AC 48 ms
61,840 KB
testcase_33 AC 69 ms
71,880 KB
testcase_34 AC 52 ms
64,572 KB
testcase_35 AC 41 ms
55,076 KB
testcase_36 AC 42 ms
56,444 KB
testcase_37 AC 42 ms
55,320 KB
testcase_38 AC 41 ms
54,380 KB
testcase_39 AC 42 ms
55,036 KB
testcase_40 AC 42 ms
54,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

input = lambda: sys.stdin.readline().rstrip()
from collections import defaultdict


class UnionFind:
    def __init__(self, N):
        self.N = N
        self.parent_or_size = [-1] * N
        self.group_count = N

    def root(self, x):
        if self.parent_or_size[x] < 0:
            return x
        else:
            self.parent_or_size[x] = self.root(self.parent_or_size[x])
            return self.parent_or_size[x]

    def unite(self, x, y):
        root_x = self.root(x)
        root_y = self.root(y)
        if root_x == root_y:
            return
        if -self.parent_or_size[root_x] < -self.parent_or_size[root_y]:
            root_x, root_y = root_y, root_x
        self.parent_or_size[root_x] += self.parent_or_size[root_y]
        self.parent_or_size[root_y] = root_x
        self.group_count -= 1

    def same(self, x, y):
        return self.root(x) == self.root(y)

    def size(self, x):
        return -self.parent_or_size[self.root(x)]

    def groups(self):
        ret = defaultdict(list)
        for i in range(self.N):
            ret[self.root(i)].append(i)
        return [group for group in ret.values()]


input = lambda: sys.stdin.readline().rstrip()

SHIFT = 20
MASK = (1 << SHIFT) - 1


def conv(x, y):
    return x << SHIFT | y


def rev(pos):
    return pos >> SHIFT, pos & MASK


H, W, N, D = map(int, input().split())

star = {}
XY = []
for i in range(N):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    XY.append((x, y))
    star[conv(x, y)] = i

MOVES = []
for xd in range(-D, D + 1):
    for yd in range(-D, D + 1):
        d = abs(xd) + abs(yd)
        if d > D:
            continue
        MOVES.append((xd, yd))


uf = UnionFind(N)

for i, (x, y) in enumerate(XY):
    for xd, yd in MOVES:
        d = abs(xd) + abs(yd)
        xn = x + xd
        yn = y + yd
        if not (0 <= xn < H and 0 <= yn < W):
            continue
        posn = conv(xn, yn)
        if posn in star:
            uf.unite(i, star[posn])

stella = uf.groups()
is_seiza = [0] * N


count = 0
for block in stella:
    if len(block) > 1:
        count += 1
        for i in block:
            is_seiza[i] = 1

mi = N
ma = 0


for x in range(H):
    for y in range(W):
        if conv(x, y) in star:
            continue
        seen = set()
        flag = False
        for xd, yd in MOVES:
            d = abs(xd) + abs(yd)
            xn = x + xd
            yn = y + yd
            if not (0 <= xn < H and 0 <= yn < W):
                continue
            posn = conv(xn, yn)
            if posn not in star:
                continue
            j = uf.root(star[posn])
            if is_seiza[j]:
                seen.add(j)
            else:
                flag = True
        if seen:
            c = count - (len(seen) - 1)
        elif flag:
            c = count + 1
        else:
            c = count

        mi = min(mi, c)
        ma = max(ma, c)
print(mi, ma)
0