結果

問題 No.2473 Fraises dans une boîte
ユーザー 👑 rin204rin204
提出日時 2023-07-30 15:55:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,460 ms / 4,000 ms
コード長 1,288 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 82,560 KB
実行使用メモリ 80,672 KB
最終ジャッジ日時 2024-04-24 23:23:14
合計ジャッジ時間 33,596 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
52,352 KB
testcase_01 AC 43 ms
52,352 KB
testcase_02 AC 45 ms
52,608 KB
testcase_03 AC 43 ms
51,968 KB
testcase_04 AC 43 ms
52,480 KB
testcase_05 AC 43 ms
52,096 KB
testcase_06 AC 82 ms
72,576 KB
testcase_07 AC 89 ms
75,264 KB
testcase_08 AC 96 ms
76,800 KB
testcase_09 AC 100 ms
76,416 KB
testcase_10 AC 99 ms
76,416 KB
testcase_11 AC 44 ms
52,608 KB
testcase_12 AC 50 ms
52,224 KB
testcase_13 AC 98 ms
76,544 KB
testcase_14 AC 92 ms
76,544 KB
testcase_15 AC 94 ms
76,288 KB
testcase_16 AC 94 ms
76,544 KB
testcase_17 AC 99 ms
76,416 KB
testcase_18 AC 94 ms
76,544 KB
testcase_19 AC 98 ms
76,544 KB
testcase_20 AC 111 ms
76,800 KB
testcase_21 AC 94 ms
76,672 KB
testcase_22 AC 100 ms
76,416 KB
testcase_23 AC 100 ms
76,544 KB
testcase_24 AC 101 ms
76,248 KB
testcase_25 AC 99 ms
76,160 KB
testcase_26 AC 101 ms
76,160 KB
testcase_27 AC 99 ms
76,604 KB
testcase_28 AC 102 ms
76,160 KB
testcase_29 AC 101 ms
75,904 KB
testcase_30 AC 96 ms
76,416 KB
testcase_31 AC 97 ms
76,176 KB
testcase_32 AC 101 ms
76,672 KB
testcase_33 AC 97 ms
76,340 KB
testcase_34 AC 98 ms
76,184 KB
testcase_35 AC 105 ms
76,416 KB
testcase_36 AC 96 ms
76,288 KB
testcase_37 AC 97 ms
76,544 KB
testcase_38 AC 96 ms
76,160 KB
testcase_39 AC 106 ms
76,444 KB
testcase_40 AC 172 ms
77,568 KB
testcase_41 AC 187 ms
77,492 KB
testcase_42 AC 142 ms
76,844 KB
testcase_43 AC 498 ms
78,492 KB
testcase_44 AC 502 ms
78,528 KB
testcase_45 AC 468 ms
78,384 KB
testcase_46 AC 838 ms
79,212 KB
testcase_47 AC 871 ms
79,224 KB
testcase_48 AC 871 ms
80,124 KB
testcase_49 AC 1,358 ms
80,440 KB
testcase_50 AC 999 ms
80,664 KB
testcase_51 AC 1,444 ms
80,372 KB
testcase_52 AC 1,355 ms
80,672 KB
testcase_53 AC 1,378 ms
80,372 KB
testcase_54 AC 1,234 ms
79,808 KB
testcase_55 AC 1,339 ms
80,496 KB
testcase_56 AC 1,056 ms
80,292 KB
testcase_57 AC 1,281 ms
80,312 KB
testcase_58 AC 966 ms
80,004 KB
testcase_59 AC 947 ms
80,000 KB
testcase_60 AC 1,460 ms
79,964 KB
testcase_61 AC 1,108 ms
80,268 KB
testcase_62 AC 1,220 ms
80,236 KB
testcase_63 AC 1,004 ms
80,268 KB
testcase_64 AC 971 ms
79,616 KB
testcase_65 AC 1,447 ms
80,096 KB
testcase_66 AC 957 ms
80,384 KB
testcase_67 AC 835 ms
79,240 KB
testcase_68 AC 1,115 ms
79,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

h, w = map(int, input().split())
S = [list(map(int, input().split())) for _ in range(h)]
cum = [[0] * (w + 1) for _ in range(h + 1)]
for i in range(h):
    for j in range(w):
        cum[i + 1][j + 1] = cum[i + 1][j] + cum[i][j + 1] - cum[i][j] + S[i][j]


def f(i0, i1, j0, j1):
    return cum[i1][j1] - cum[i0][j1] - cum[i1][j0] + cum[i0][j0]


zz = [w] * (h + 1)
for i in range(h + 1):
    while zz[i] > 0 and f(i, h, zz[i] - 1, w) == 0:
        zz[i] -= 1

dp = [[1 << 30] * (w + 1) for _ in range(h + 1)]
dp[h][w] = 0

for i in range(h, -1, -1):
    for j in range(w, -1, -1):
        if i != h and f(i, i + 1, j, w) == 0:
            dp[i][j] = min(dp[i][j], dp[i + 1][j])
        if j != w and f(i, h, j, j + 1) == 0:
            dp[i][j] = min(dp[i][j], dp[i][j + 1])

        row = [0] * (h + 1)
        col = [0] * (w + 1)
        for i2 in range(i, h):
            row[i2 + 1] = row[i2] + int(f(i2, i2 + 1, j, w) > 0)
        for j2 in range(j, w):
            col[j2 + 1] = col[j2] + int(f(i, h, j2, j2 + 1) > 0)

        for i2 in range(i + 1, h + 1):
            j2 = max(j + 1, zz[i2])
            if j2 > w:
                continue
            tmp = dp[i2][j] + dp[i][j2] + (row[i2] * col[j2] - f(i, i2, j, j2))
            dp[i][j] = min(dp[i][j], tmp)

print(dp[0][0])
0