結果

問題 No.2696 Sign Creation
ユーザー mo124121mo124121
提出日時 2024-02-04 10:03:30
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,882 bytes
コンパイル時間 328 ms
コンパイル使用メモリ 82,044 KB
実行使用メモリ 208,220 KB
最終ジャッジ日時 2024-12-20 11:52:27
合計ジャッジ時間 14,655 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import sys

sys.setrecursionlimit(2 * 10**3)

if sys.implementation.name == "pypy":
    import pypyjit

    pypyjit.set_param("max_unroll_recursion=-1")


from collections import Counter, 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())
    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)

cand = Counter()
for i, (x, y) in enumerate(XY):
    for xd, yd in MOVES:
        d = abs(xd) + abs(yd)
        if d > D:
            continue
        xn = x + xd
        yn = y + yd
        if xn < 0 or yn < 0:
            continue
        posn = conv(xn, yn)
        cand[posn] += 1
        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 = count
ma = count

for pos in cand:
    x, y = rev(pos)
    seen = set()
    if cand[pos] == 1:
        if uf.size(star[pos]) == 1:
            ma = max(ma, count + 1)
        continue
    for xd, yd in MOVES:
        d = abs(xd) + abs(yd)
        if d > D:
            continue
        xn = x + xd
        yn = y + yd
        posn = conv(xn, yn)
        if posn not in star:
            continue
        seen.add(uf.root(star[posn]))
    c = count + 1
    for s in seen:
        if is_seiza[s]:
            c -= 1
    mi = min(mi, c)
    ma = max(ma, c)
print(mi, ma)
0