結果

問題 No.2456 Stamp Art
ユーザー LyricalMaestro
提出日時 2024-11-11 01:25:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,907 ms / 5,000 ms
コード長 1,929 bytes
コンパイル時間 599 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 280,160 KB
最終ジャッジ日時 2024-11-11 01:25:48
合計ジャッジ時間 39,201 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

# https://yukicoder.me/problems/no/2456


def solve(H, W, S, replica_cells, k):
    stamped_cell = [[0] * (W + 1) for _ in range(H + 1)]

    for h in range(k, H + 1):
        for w in range(k, W + 1):
            b = replica_cells[h][w] - replica_cells[h - k][w] - replica_cells[h][w - k] + replica_cells[h - k][w - k]
            if b == k ** 2:
                stamped_cell[h][w] += 1
                stamped_cell[h - k][w] -= 1
                stamped_cell[h][w - k] -= 1
                stamped_cell[h - k][w - k] += 1
    
    for h in range(H + 1):
        ans = 0
        for w in reversed(range(W + 1)):
            ans += stamped_cell[h][w]
            stamped_cell[h][w] = ans

    for w in range(W + 1):
        ans = 0
        for h in reversed(range(H + 1)):
            ans += stamped_cell[h][w]
            stamped_cell[h][w] = ans

    for h in range(H):
        for w in range(W):
            if S[h][w] == "#" and stamped_cell[h + 1][w + 1] == 0:
                return False
            elif S[h][w] == "." and stamped_cell[h + 1][w + 1] > 0:
                return False
    return True


def main():
    H, W = map(int, input().split())
    S = []
    for _ in range(H):
        S.append(input())
    

    replica_cells = [[0] * (W + 1) for _ in range(H + 1)]
    for h in range(H):
        for w in range(W):
            if S[h][w] == "#":
                replica_cells[h + 1][w + 1] = 1

    for h in range(H):
        row = 0
        for w in range(W):
            row += replica_cells[h + 1][w + 1]
            replica_cells[h + 1][w + 1] = row + replica_cells[h][w + 1]

    low = 1
    high = min(H, W)
    while high - low > 1:
        mid = (high + low) // 2
        if solve(H, W, S, replica_cells, mid):
            low = mid
        else:
            high =mid
    if solve(H, W, S, replica_cells, high):
        print(high)
    else:
        print(low)




if __name__ == "__main__":
    main()
0