結果

問題 No.2731 Two Colors
ユーザー ShirotsumeShirotsume
提出日時 2024-04-19 22:52:08
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,716 bytes
コンパイル時間 401 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 131,272 KB
最終ジャッジ日時 2024-04-19 22:52:38
合計ジャッジ時間 21,787 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 WA -
testcase_33 AC 42 ms
55,936 KB
testcase_34 AC 43 ms
56,064 KB
testcase_35 AC 41 ms
55,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, time, random
from collections import deque, Counter, defaultdict
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 61 - 1
mod = 998244353
import heapq
class Heap():
    def __init__(self, maxi = False):
        self.sum = 0
        self.al = []
        self.maxi = maxi
        self.len = 0

    def __len__(self):
        return len(self.al)

    def add(self, x):
        if self.maxi:
            heapq.heappush(self.al, -x)
        else:
            heapq.heappush(self.al, x)
    
    def pop(self):
        if self.maxi:
            now = heapq.heappop(self.al)
            return -now
        else:
            now = heapq.heappop(self.al)
            return now

    def top(self):
        now = self.pop()
        self.add(now)
        return now
h, w = mi()
a = [li() for _ in range(h)]

zero = Heap()
one = Heap()

color = [[-1] * w for _ in range(h)]


zero.add((a[0][0], 0, 0))
one.add((a[h - 1][w - 1], h - 1, w - 1))
cnt = 0
for i in range(h * w):
    if i % 2 == 0:
        now, x, y = zero.pop()
    else:
        now, x, y = one.pop()
    if color[x][y] != -1:
        continue
    cnt += 1
    color[x][y] = i % 2
    f = True
    for tox, toy in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
        nx, ny = x + tox, y + toy
        if 0 <= nx < h and 0 <= ny < w and color[nx][ny] == -1:
            if i % 2 == 0:
                zero.add((a[nx][ny], nx, ny))
            else:
                one.add((a[nx][ny], nx, ny))
        elif 0 <= nx < h and 0 <= ny < w and color[nx][ny] != color[x][y]:
            f = False
            break
    if not f:
        break
print(cnt - 2)
    
    
    
0