結果

問題 No.1606 Stuffed Animals Keeper
ユーザー んんんんんん
提出日時 2022-12-30 12:16:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 112 ms / 3,000 ms
コード長 1,005 bytes
コンパイル時間 2,058 ms
コンパイル使用メモリ 204,664 KB
実行使用メモリ 101,000 KB
最終ジャッジ日時 2023-08-16 19:16:46
合計ジャッジ時間 5,406 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 25 ms
22,780 KB
testcase_04 AC 30 ms
28,176 KB
testcase_05 AC 7 ms
7,776 KB
testcase_06 AC 10 ms
11,156 KB
testcase_07 AC 21 ms
19,400 KB
testcase_08 AC 19 ms
19,132 KB
testcase_09 AC 8 ms
9,452 KB
testcase_10 AC 5 ms
6,720 KB
testcase_11 AC 3 ms
4,868 KB
testcase_12 AC 11 ms
10,892 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 112 ms
101,000 KB
testcase_16 AC 38 ms
36,308 KB
testcase_17 AC 38 ms
36,816 KB
testcase_18 AC 39 ms
36,028 KB
testcase_19 AC 38 ms
36,084 KB
testcase_20 AC 39 ms
36,100 KB
testcase_21 AC 6 ms
6,528 KB
testcase_22 AC 5 ms
6,192 KB
testcase_23 AC 5 ms
5,932 KB
testcase_24 AC 6 ms
6,520 KB
testcase_25 AC 6 ms
6,312 KB
testcase_26 AC 4 ms
4,380 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 AC 3 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 3 ms
4,380 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 3 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 3 ms
4,376 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 38 ms
35,716 KB
testcase_44 AC 39 ms
35,792 KB
testcase_45 AC 38 ms
35,836 KB
testcase_46 AC 38 ms
35,756 KB
testcase_47 AC 40 ms
35,768 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,376 KB
testcase_50 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {
    int N;
    cin >> N;
    vector<int> A(N);
    for (int i = 0; i < N; i++) cin >> A[i];

    vector<int> len, cnt;
    int s = -1;
    int c = 0;
    for (int i = 0; i <= N; i++) {
        if (i == N || A[i] == 2) {
            len.push_back(i - s - 1);
            cnt.push_back(c);
            s = i;
            c = 0;
        }
        if (i != N && A[i] == 1) c++;
    }

    int M = len.size();
    vector<vector<int>> dp(M+1, vector<int>(N+1, 1 << 30));
    dp[0][0] = 0;
    for (int i = 0; i < M; i++) {
        for (int j = N; j >= 0; j--) {
            if (j - len[i] >= 0 && dp[i][j-len[i]] != 1 << 30) {
                dp[i+1][j] = min(dp[i+1][j], dp[i][j-len[i]] + cnt[i]);
            }
            dp[i+1][j] = min(dp[i+1][j], dp[i][j]);
        }
    }

    int count = 0;
    for (int i = 0; i < N; i++) if (A[i] == 0) count++;

    int ans = dp[M][count];
    if (ans == 1 << 30) ans = -1;
    
    cout << ans << endl;
}
0