結果

問題 No.2456 Stamp Art
ユーザー mkawa2mkawa2
提出日時 2023-09-09 10:16:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,879 ms / 5,000 ms
コード長 3,612 bytes
コンパイル時間 982 ms
コンパイル使用メモリ 87,004 KB
実行使用メモリ 298,316 KB
最終ジャッジ日時 2023-09-09 10:17:26
合計ジャッジ時間 55,052 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,380 KB
testcase_01 AC 70 ms
71,332 KB
testcase_02 AC 70 ms
71,044 KB
testcase_03 AC 3,133 ms
295,536 KB
testcase_04 AC 107 ms
71,324 KB
testcase_05 AC 89 ms
76,796 KB
testcase_06 AC 3,044 ms
298,024 KB
testcase_07 AC 3,099 ms
298,264 KB
testcase_08 AC 2,772 ms
298,316 KB
testcase_09 AC 3,212 ms
297,988 KB
testcase_10 AC 3,868 ms
298,032 KB
testcase_11 AC 3,029 ms
298,240 KB
testcase_12 AC 3,110 ms
297,776 KB
testcase_13 AC 3,845 ms
298,216 KB
testcase_14 AC 3,879 ms
298,216 KB
testcase_15 AC 2,844 ms
297,980 KB
testcase_16 AC 1,458 ms
250,104 KB
testcase_17 AC 91 ms
76,816 KB
testcase_18 AC 76 ms
75,988 KB
testcase_19 AC 1,911 ms
277,160 KB
testcase_20 AC 3,294 ms
298,028 KB
testcase_21 AC 3,143 ms
276,316 KB
testcase_22 AC 3,504 ms
298,004 KB
testcase_23 AC 193 ms
104,364 KB
testcase_24 AC 374 ms
109,720 KB
testcase_25 AC 73 ms
71,100 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(1000005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = -1-(-1 << 63)
# md = 10**9+7
md = 998244353

class CumSum2D:
    def __init__(self, arr, w=None):
        if w:
            self.h = arr
            self.w = w
        else:
            self.h = len(arr)
            self.w = len(arr[0])
        self.t = [0]*(self.h+1)*(self.w+1)
        if w == None:
            w = self.w+1
            for i, row in enumerate(arr):
                self.t[(i+1)*w+1:(i+1)*w+w] = row
        self._finalized = False

    def add(self, i, j, c):
        w = self.w+1
        self.t[(i+1)*w+j+1] += c

    def finalize(self):
        w = self.w+1
        self._finalized = True
        for ij in range(w+1, (self.h+1)*w):
            i, j = divmod(ij, w)
            if j == 0: continue
            if j+1 < w: self.t[i*w+j+1] += self.t[i*w+j]
            self.t[i*w+j] += self.t[(i-1)*w+j]

    def sum(self, ij0, ij1=None):
        if not self._finalized:
            print("Finalize!!!")
            exit()
        w = self.w+1
        i, j = ij0
        x, y = self.h, self.w
        if ij1: x, y = ij1
        if i >= x or j >= y: return 0
        return self.t[i*w+j]+self.t[x*w+y]-self.t[i*w+y]-self.t[x*w+j]

class Imos2D:
    def __init__(self, arr, w=None):
        if w:
            self.h = arr
            self.w = w
        else:
            self.h = len(arr)
            self.w = len(arr[0])
        self.t = [0]*(self.h+1)*(self.w+1)
        if w == None:
            w = self.w+1
            for i, row in enumerate(arr):
                self.t[i*w:i*w+w-1] = row
        self._finalized = False

    def add(self, ij0, ij1, c):
        i, j = ij0
        x, y = ij1
        w = self.w+1
        self.t[i*w+j] += c
        self.t[x*w+y] += c
        self.t[i*w+y] -= c
        self.t[x*w+j] -= c

    def finalize(self):
        w = self.w+1
        self._finalized = True
        for ij in range(self.h*w):
            i, j = divmod(ij, w)
            if j == w-1: continue
            self.t[i*w+j+1] += self.t[i*w+j]
            if i: self.t[i*w+j] += self.t[(i-1)*w+j]

    def val(self, i, j):
        if not self._finalized:
            print("Finalize!!!")
            exit()
        return self.t[i*(self.w+1)+j]

def binary_search(l, r, minimize):
    if minimize: l -= 1
    else: r += 1
    while l+1 < r:
        m = (l+r)//2
        if ok(m) ^ minimize: l = m
        else: r = m
    if minimize: return r
    return l

def ok(m):
    m2 = m**2
    cur = Imos2D(h, w)
    for i in range(h-m+1):
        for j in range(w-m+1):
            if cs.sum([i, j], [i+m, j+m]) == m2:
                cur.add([i, j], [i+m, j+m], 1)
    cur.finalize()
    for i in range(h):
        for j in range(w):
            if (cur.val(i, j) > 0) != (ss[i][j] > 0): return False
    return True

h, w = LI()
ss = [[(c == "#")*1 for c in SI()] for _ in range(h)]
cs = CumSum2D(ss)
cs.finalize()
tt = []
for s in ss: tt += s+[0]

print(binary_search(1, min(h, w), False))
0