結果

問題 No.2657 Falling Block Game
ユーザー 👑 rin204rin204
提出日時 2024-03-03 16:43:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 483 ms / 2,000 ms
コード長 2,727 bytes
コンパイル時間 529 ms
コンパイル使用メモリ 81,840 KB
実行使用メモリ 196,572 KB
最終ジャッジ日時 2024-09-29 16:54:59
合計ジャッジ時間 15,576 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,764 KB
testcase_01 AC 35 ms
53,852 KB
testcase_02 AC 38 ms
53,652 KB
testcase_03 AC 38 ms
53,264 KB
testcase_04 AC 195 ms
81,248 KB
testcase_05 AC 471 ms
168,160 KB
testcase_06 AC 324 ms
141,396 KB
testcase_07 AC 388 ms
196,572 KB
testcase_08 AC 242 ms
82,952 KB
testcase_09 AC 312 ms
81,716 KB
testcase_10 AC 331 ms
151,372 KB
testcase_11 AC 337 ms
160,416 KB
testcase_12 AC 339 ms
157,488 KB
testcase_13 AC 336 ms
149,152 KB
testcase_14 AC 342 ms
157,508 KB
testcase_15 AC 341 ms
156,960 KB
testcase_16 AC 339 ms
160,460 KB
testcase_17 AC 334 ms
150,072 KB
testcase_18 AC 340 ms
154,536 KB
testcase_19 AC 340 ms
154,808 KB
testcase_20 AC 385 ms
81,712 KB
testcase_21 AC 381 ms
81,444 KB
testcase_22 AC 347 ms
80,812 KB
testcase_23 AC 355 ms
81,216 KB
testcase_24 AC 354 ms
81,236 KB
testcase_25 AC 355 ms
80,932 KB
testcase_26 AC 360 ms
81,300 KB
testcase_27 AC 346 ms
80,532 KB
testcase_28 AC 358 ms
81,328 KB
testcase_29 AC 345 ms
80,272 KB
testcase_30 AC 443 ms
82,940 KB
testcase_31 AC 435 ms
82,880 KB
testcase_32 AC 456 ms
83,288 KB
testcase_33 AC 423 ms
82,428 KB
testcase_34 AC 430 ms
82,668 KB
testcase_35 AC 436 ms
82,892 KB
testcase_36 AC 435 ms
82,832 KB
testcase_37 AC 483 ms
82,412 KB
testcase_38 AC 455 ms
82,396 KB
testcase_39 AC 437 ms
82,916 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