結果

問題 No.2657 Falling Block Game
ユーザー 👑 rin204rin204
提出日時 2024-03-03 16:43:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 548 ms / 2,000 ms
コード長 2,727 bytes
コンパイル時間 2,495 ms
コンパイル使用メモリ 81,444 KB
実行使用メモリ 196,404 KB
最終ジャッジ日時 2024-03-03 16:43:45
合計ジャッジ時間 18,863 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,332 KB
testcase_01 AC 37 ms
53,332 KB
testcase_02 AC 37 ms
53,332 KB
testcase_03 AC 38 ms
53,332 KB
testcase_04 AC 214 ms
80,868 KB
testcase_05 AC 520 ms
167,828 KB
testcase_06 AC 377 ms
140,856 KB
testcase_07 AC 465 ms
196,404 KB
testcase_08 AC 295 ms
82,556 KB
testcase_09 AC 413 ms
81,124 KB
testcase_10 AC 391 ms
155,052 KB
testcase_11 AC 392 ms
160,136 KB
testcase_12 AC 388 ms
157,076 KB
testcase_13 AC 382 ms
149,764 KB
testcase_14 AC 395 ms
157,348 KB
testcase_15 AC 394 ms
157,184 KB
testcase_16 AC 399 ms
159,788 KB
testcase_17 AC 383 ms
151,324 KB
testcase_18 AC 392 ms
157,064 KB
testcase_19 AC 390 ms
155,668 KB
testcase_20 AC 452 ms
81,596 KB
testcase_21 AC 427 ms
80,948 KB
testcase_22 AC 436 ms
81,080 KB
testcase_23 AC 414 ms
81,076 KB
testcase_24 AC 422 ms
80,916 KB
testcase_25 AC 424 ms
80,980 KB
testcase_26 AC 426 ms
81,056 KB
testcase_27 AC 406 ms
80,264 KB
testcase_28 AC 422 ms
80,664 KB
testcase_29 AC 415 ms
80,088 KB
testcase_30 AC 548 ms
82,716 KB
testcase_31 AC 520 ms
82,636 KB
testcase_32 AC 521 ms
82,472 KB
testcase_33 AC 537 ms
82,380 KB
testcase_34 AC 512 ms
82,516 KB
testcase_35 AC 513 ms
82,092 KB
testcase_36 AC 512 ms
82,928 KB
testcase_37 AC 519 ms
82,000 KB
testcase_38 AC 515 ms
82,116 KB
testcase_39 AC 527 ms
82,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq


class DeletableHeapq:
    def __init__(self, lst=None, reverse=False):
        if reverse:
            self.pm = -1
        else:
            self.pm = 1
        if lst is None:
            self.hq = []
        else:
            self.hq = [self.pm * x for x in lst]

        heapq.heapify(self.hq)
        self.tot = sum(self.hq) * self.pm
        self.cnt = {}
        for x in self.hq:
            self.cnt[x] = self.cnt.get(x, 0) + 1
        self.length = len(self.hq)

    def __bool__(self):
        return bool(self.hq)

    def __len__(self):
        return self.length

    @property
    def sum(self):
        return self.tot

    def top(self):
        if self.hq:
            return self.hq[0] * self.pm
        else:
            return None

    def __getitem__(self, i):
        # 先頭要素にアクセスしたいときのみ
        assert i == 0
        return self.top()

    def push(self, x):
        self.length += 1
        self.cnt[x * self.pm] = self.cnt.get(x * self.pm, 0) + 1
        self.tot += x
        heapq.heappush(self.hq, x * self.pm)

    def pop(self):
        if not self.hq:
            return None
        self.length -= 1
        x = heapq.heappop(self.hq)
        self.cnt[x] -= 1
        self.tot -= x * self.pm
        self._delete()
        return x * self.pm

    def remove(self, x):
        if self.cnt.get(x * self.pm, 0) == 0:
            return False
        self.length -= 1
        self.cnt[x * self.pm] -= 1
        self.tot -= x
        self._delete()
        return True

    def _delete(self):
        while self.hq and self.cnt.get(self.hq[0], 0) == 0:
            heapq.heappop(self.hq)


h, w = map(int, input().split())
S = [input() for _ in range(h)]
dp = [0] * w
for i in range(h - 1, -1, -1):
    add = [[] for _ in range(w)]
    rem = [[] for _ in range(w)]
    L = [0] * w
    l = 0
    for j in range(w):
        if S[i][j] == "#":
            l = j + 1
        L[j] = l
    R = [0] * w
    r = w - 1
    for j in range(w - 1, -1, -1):
        if S[i][j] == "#":
            r = j - 1
        R[j] = r

    for j in range(w):
        if S[i][j] == "#":
            continue
        l = max(L[j], j - dp[j])
        r = min(R[j], j + dp[j])
        add[l].append(dp[j])
        rem[r].append(dp[j])

    hq = DeletableHeapq()
    hq.push(1 << 30)
    dp = [1 << 30] * w
    for j in range(w):
        for a in add[j]:
            hq.push(a)
        dp[j] = hq[0]
        for a in rem[j]:
            hq.remove(a)

    for j in range(1, w):
        if S[i][j] != "#":
            dp[j] = min(dp[j], dp[j - 1] + 1)
    for j in range(w - 2, -1, -1):
        if S[i][j] != "#":
            dp[j] = min(dp[j], dp[j + 1] + 1)


print(*dp, sep="\n")
0