結果

問題 No.2696 Sign Creation
ユーザー flygonflygon
提出日時 2024-03-22 22:55:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 472 ms / 2,500 ms
コード長 2,507 bytes
コンパイル時間 182 ms
コンパイル使用メモリ 82,892 KB
実行使用メモリ 83,488 KB
最終ジャッジ日時 2024-05-17 18:17:33
合計ジャッジ時間 7,952 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
63,956 KB
testcase_01 AC 51 ms
64,872 KB
testcase_02 AC 42 ms
56,572 KB
testcase_03 AC 47 ms
62,396 KB
testcase_04 AC 47 ms
62,964 KB
testcase_05 AC 47 ms
62,356 KB
testcase_06 AC 43 ms
57,204 KB
testcase_07 AC 40 ms
56,236 KB
testcase_08 AC 132 ms
78,492 KB
testcase_09 AC 85 ms
76,780 KB
testcase_10 AC 150 ms
78,736 KB
testcase_11 AC 80 ms
76,780 KB
testcase_12 AC 81 ms
76,840 KB
testcase_13 AC 415 ms
81,312 KB
testcase_14 AC 371 ms
82,220 KB
testcase_15 AC 312 ms
81,132 KB
testcase_16 AC 373 ms
80,136 KB
testcase_17 AC 172 ms
79,228 KB
testcase_18 AC 239 ms
80,664 KB
testcase_19 AC 326 ms
81,360 KB
testcase_20 AC 94 ms
77,244 KB
testcase_21 AC 259 ms
80,856 KB
testcase_22 AC 279 ms
81,572 KB
testcase_23 AC 379 ms
82,584 KB
testcase_24 AC 365 ms
83,488 KB
testcase_25 AC 333 ms
81,460 KB
testcase_26 AC 235 ms
81,752 KB
testcase_27 AC 210 ms
80,976 KB
testcase_28 AC 207 ms
80,412 KB
testcase_29 AC 180 ms
79,500 KB
testcase_30 AC 203 ms
79,720 KB
testcase_31 AC 472 ms
80,856 KB
testcase_32 AC 48 ms
62,776 KB
testcase_33 AC 64 ms
72,884 KB
testcase_34 AC 53 ms
67,040 KB
testcase_35 AC 42 ms
56,052 KB
testcase_36 AC 40 ms
55,460 KB
testcase_37 AC 40 ms
55,988 KB
testcase_38 AC 40 ms
56,096 KB
testcase_39 AC 43 ms
55,996 KB
testcase_40 AC 39 ms
55,704 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(5*10**5)
input = sys.stdin.readline
from collections import defaultdict, deque, Counter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right
from math import gcd
from collections import defaultdict


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.p = [-1] * (n+1)

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.p[x] > self.p[y]:
            x, y = y, x
        self.p[x] += self.p[y]
        self.p[y] = x

    def same(self, a, b):
        return self.find(a) == self.find(b)

    def group(self):
        d = defaultdict(list)
        for i in range(1, self.n+1):
            par = self.find(i)
            d[par].append(i)
        return d

h,w,n,d = map(int,input().split())
g = [[-1]*w for i in range(h)]

now = 1
for i in range(n):
    n,m = map(int,input().split())
    g[n-1][m-1] = i

uf = UnionFind(h*w+1)
for i in range(h):
    for j in range(w):
        if g[i][j] == -1: continue
        for x in range(i-d,i+d+1):
            for y in range(j-d, j+d+1):
                if not (0<=x<h and 0<=y<w): continue
                if g[x][y] == -1: continue
                if(abs(x-i) + abs(y-j)) > d:continue
                uf.union(x*w+y, i*w+j)

cnt = [0]*(h*w)
s = set()
for i in range(h):
    for j in range(w):
        if g[i][j] == -1: continue
        g[i][j] = uf.find(i*w+j)
        s.add(uf.find(i*w+j))
        cnt[uf.find(i*w+j)] += 1

tot = len(s) - cnt.count(1)
mn = tot
mx = tot
for i in range(h):
    for j in range(w):
        if g[i][j] != -1: continue
        two = 0
        one = 0
        wa = set()
        for x in range(i-d,i+d+1):
            for y in range(j-d, j+d+1):
                if not (0<=x<h and 0<=y<w): continue
                if g[x][y] == -1: continue
                if g[x][y] in wa: continue
                if(abs(x-i) + abs(y-j)) > d:continue
                wa.add(g[x][y])
                if cnt[g[x][y]] == 1:
                    one += 1
                else:
                    two += 1
        tmp = tot
        if two >= 1:
            tmp -= two-1
        else:
            if one:
                tmp += 1                    
        mn = min(tmp, mn)
        mx = max(tmp, mx)
print(mn,mx)
                
0