結果

問題 No.2473 Fraises dans une boîte
ユーザー rin204rin204
提出日時 2023-07-30 15:48:49
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 135 ms / 4,000 ms
コード長 1,662 bytes
コンパイル時間 4,102 ms
コンパイル使用メモリ 281,400 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 23:25:12
合計ジャッジ時間 8,776 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 7 ms
5,376 KB
testcase_41 AC 7 ms
5,376 KB
testcase_42 AC 7 ms
5,376 KB
testcase_43 AC 40 ms
5,376 KB
testcase_44 AC 40 ms
5,376 KB
testcase_45 AC 40 ms
5,376 KB
testcase_46 AC 79 ms
5,376 KB
testcase_47 AC 79 ms
5,376 KB
testcase_48 AC 79 ms
5,376 KB
testcase_49 AC 133 ms
5,376 KB
testcase_50 AC 131 ms
5,376 KB
testcase_51 AC 134 ms
5,376 KB
testcase_52 AC 133 ms
5,376 KB
testcase_53 AC 135 ms
5,376 KB
testcase_54 AC 133 ms
5,376 KB
testcase_55 AC 134 ms
5,376 KB
testcase_56 AC 132 ms
5,376 KB
testcase_57 AC 133 ms
5,376 KB
testcase_58 AC 131 ms
5,376 KB
testcase_59 AC 132 ms
5,376 KB
testcase_60 AC 134 ms
5,376 KB
testcase_61 AC 132 ms
5,376 KB
testcase_62 AC 134 ms
5,376 KB
testcase_63 AC 133 ms
5,376 KB
testcase_64 AC 132 ms
5,376 KB
testcase_65 AC 134 ms
5,376 KB
testcase_66 AC 132 ms
5,376 KB
testcase_67 AC 133 ms
5,376 KB
testcase_68 AC 134 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

int main() {
    int h, w;
    cin >> h >> w;
    vector<vector<int>> S(h, vector<int>(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> S[i][j];
        }
    }

    vector<vector<int>> cum(h + 1, vector<int>(w + 1, 0));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cum[i + 1][j + 1] = cum[i + 1][j] + cum[i][j + 1] - cum[i][j] + S[i][j];
        }
    }

    auto f = [&](int i0, int i1, int j0, int j1) -> int { return cum[i1][j1] - cum[i0][j1] - cum[i1][j0] + cum[i0][j0]; };

    vector<int> zz(h + 1, w);
    for (int i = 0; i <= h; i++) {
        while (zz[i] > 0 && f(i, h, zz[i] - 1, w) == 0) zz[i]--;
    }

    vector<vector<int>> dp(h + 1, vector<int>(w + 1, 1 << 30));
    dp[h][w] = 0;

    for (int i = h; i >= 0; i--) {
        for (int j = w; j >= 0; j--) {
            if (i != h && f(i, i + 1, j, w) == 0) dp[i][j] = min(dp[i][j], dp[i + 1][j]);
            if (j != w && f(i, h, j, j + 1) == 0) dp[i][j] = min(dp[i][j], dp[i][j + 1]);

            vector<int> row(h + 1, 0);
            vector<int> col(w + 1, 0);
            for (int i2 = i; i2 < h; i2++) row[i2 + 1] = row[i2] + (f(i2, i2 + 1, j, w) > 0 ? 1 : 0);
            for (int j2 = j; j2 < w; j2++) col[j2 + 1] = col[j2] + (f(i, h, j2, j2 + 1) > 0 ? 1 : 0);

            for (int i2 = i; i2 <= h; i2++) {
                int j2   = max(j + 1, zz[i2]);
                int tmp  = dp[i2][j] + dp[i][j2] + row[i2] * col[j2] - f(i, i2, j, j2);
                dp[i][j] = min(dp[i][j], tmp);
            }
        }
    }

    cout << dp[0][0] << endl;
}
0