結果

問題 No.2696 Sign Creation
ユーザー mo124121mo124121
提出日時 2024-02-08 08:36:45
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,801 bytes
コンパイル時間 191 ms
コンパイル使用メモリ 82,552 KB
実行使用メモリ 184,288 KB
最終ジャッジ日時 2024-05-17 18:10:01
合計ジャッジ時間 6,626 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 44 ms
54,452 KB
testcase_33 AC 44 ms
55,500 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 AC 44 ms
54,512 KB
testcase_38 AC 43 ms
56,496 KB
testcase_39 WA -
testcase_40 WA -
権限があれば一括ダウンロードができます

ソースコード

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

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


uf = UnionFind(N)
cand = []
for i, (x, y) in enumerate(XY):
    for xd, yd in NEIBOR:
        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])
        else:
            cand.append((x, y))

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


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

mi = N
ma = 0


for x, y in cand:
    if conv(x, y) in star:
        continue
    seen = set()
    flag = False
    for xd, yd in NEIBOR:
        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