結果
| 問題 | No.2696 Sign Creation | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2024-11-02 20:29:39 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,518 ms / 2,500 ms | 
| コード長 | 3,265 bytes | 
| コンパイル時間 | 499 ms | 
| コンパイル使用メモリ | 82,360 KB | 
| 実行使用メモリ | 107,868 KB | 
| 最終ジャッジ日時 | 2024-11-02 20:29:57 | 
| 合計ジャッジ時間 | 16,874 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 38 | 
ソースコード
## https://yukicoder.me/problems/no/2696
from collections import deque
def main():
    H, W, N, D = map(int, input().split())
    xy = []
    for _ in range(N):
        x, y = map(int, input().split())
        xy.append((x - 1, y -1))
    
    # 座標から星idを取れるようにする
    star_map = {}
    for i in range(N):
        x, y = xy[i]
        star_map[(x, y)] = i
    # 連結成分ごとに分ける
    components_list = [-1] * N
    component_id = 0
    for s in range(N):
        if components_list[s] == -1:
            queue = deque()
            queue.append(s)
            components_list[s] = component_id
            while len(queue) > 0:
                v = queue.popleft()                
                h, w = xy[v]
                for di in range(-5, 6):
                    for dj in range(-5, 6):
                        if abs(di) + abs(dj) > D:
                            continue
                        if di == 0 and dj == 0:
                            continue
                        new_h = h + di
                        new_w = w + dj
                        if 0 <= new_h < H and 0 <= new_w < W:
                            if (new_h, new_w) in star_map:
                                new_v = star_map[(new_h, new_w)] 
                                if components_list[new_v] == -1:
                                    components_list[new_v] = component_id
                                    queue.append(new_v)
            component_id += 1
    # 星の集合の中にどれくらい星があるかを入れる
    component_id_map = {}
    for i in range(N):
        c_id = components_list[i]
        if c_id not in component_id_map:
            component_id_map[c_id] = 0
        component_id_map[c_id] += 1
    
    base_size = 0
    for key, volume in component_id_map.items():
        if volume > 1:
            base_size += 1
    min_size = base_size
    max_size = base_size
    # それぞれの座標に星を置いた時の調査
    for h in range(H):
        for w in range(W):
            if (h, w) in star_map:
                continue
            
            seiza_set = set()
            b = set()
            for di in range(-5, 6):
                for dj in range(-5, 6):
                    if abs(di) + abs(dj) > D:
                        continue
                    if di == 0 and dj == 0:
                        continue
                    new_h = h + di
                    new_w = w + dj
                    if 0 <= new_h < H and 0 <= new_w < W:
                        if (new_h, new_w) in star_map:
                            i = star_map[(new_h, new_w)] 
                            x = components_list[i]
                            if component_id_map[x] == 1:
                                b.add(x)
                            else:
                                seiza_set.add(x)
            
            ans = 0
            if len(seiza_set) > 0:
                ans = 1 - len(seiza_set)
            elif len(b) > 0:
                ans = 1
            min_size = min(min_size, base_size + ans)
            max_size = max(max_size, base_size + ans)
    print(min_size, max_size)
    
                            
if __name__ == "__main__":
    main()
            
            
            
        