結果

問題 No.1606 Stuffed Animals Keeper
ユーザー ぷらぷら
提出日時 2021-07-16 22:08:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 3,000 ms
コード長 1,077 bytes
コンパイル時間 2,091 ms
コンパイル使用メモリ 203,904 KB
実行使用メモリ 197,876 KB
最終ジャッジ日時 2023-09-20 14:10:59
合計ジャッジ時間 6,611 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 57 ms
154,108 KB
testcase_04 AC 62 ms
164,168 KB
testcase_05 AC 21 ms
73,668 KB
testcase_06 AC 30 ms
100,364 KB
testcase_07 AC 47 ms
145,572 KB
testcase_08 AC 45 ms
139,484 KB
testcase_09 AC 26 ms
90,052 KB
testcase_10 AC 19 ms
67,400 KB
testcase_11 AC 12 ms
44,688 KB
testcase_12 AC 30 ms
100,436 KB
testcase_13 AC 57 ms
180,572 KB
testcase_14 AC 56 ms
180,756 KB
testcase_15 AC 107 ms
180,860 KB
testcase_16 AC 73 ms
180,908 KB
testcase_17 AC 75 ms
180,572 KB
testcase_18 AC 75 ms
180,576 KB
testcase_19 AC 74 ms
180,636 KB
testcase_20 AC 75 ms
180,584 KB
testcase_21 AC 58 ms
180,548 KB
testcase_22 AC 58 ms
180,744 KB
testcase_23 AC 58 ms
180,668 KB
testcase_24 AC 64 ms
180,660 KB
testcase_25 AC 58 ms
197,580 KB
testcase_26 AC 55 ms
197,632 KB
testcase_27 AC 55 ms
197,800 KB
testcase_28 AC 55 ms
197,688 KB
testcase_29 AC 56 ms
197,684 KB
testcase_30 AC 55 ms
197,716 KB
testcase_31 AC 55 ms
197,624 KB
testcase_32 AC 55 ms
197,668 KB
testcase_33 AC 55 ms
197,748 KB
testcase_34 AC 55 ms
197,624 KB
testcase_35 AC 55 ms
197,876 KB
testcase_36 AC 55 ms
197,660 KB
testcase_37 AC 55 ms
197,764 KB
testcase_38 AC 55 ms
197,636 KB
testcase_39 AC 55 ms
197,668 KB
testcase_40 AC 55 ms
197,808 KB
testcase_41 AC 56 ms
197,716 KB
testcase_42 AC 56 ms
197,648 KB
testcase_43 AC 73 ms
197,612 KB
testcase_44 AC 73 ms
197,852 KB
testcase_45 AC 73 ms
197,696 KB
testcase_46 AC 73 ms
197,844 KB
testcase_47 AC 73 ms
197,612 KB
testcase_48 AC 1 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;

constexpr int inf = 1001001001;

int dp[5005][10005];

int main() {
    int N;
    cin >> N;
    for(int i = 0; i <= N; i++) {
        for(int j = 0; j <= N; j++) {
            dp[i][j] = inf;
        }
    }
    int sum = 0;
    vector<int>A(N);
    pair<int,int>x;
    vector<pair<int,int>>tmp;
    for(int i = 0; i < N; i++) {
        cin >> A[i];
        if(A[i] == 2) {
            tmp.push_back(x);
            x = {0,0};
        }
        else if(A[i] == 0) {
            x.first++;
            sum++;
        }
        else {
            x.second++;
        }
    }
    tmp.push_back(x);
    dp[0][0] = 0;
    for(int i = 0; i < tmp.size(); i++) {
        for(int j = 0; j <= N; j++) {
            dp[i+1][j] = min(dp[i+1][j],dp[i][j]+tmp[i].first);
            dp[i+1][j+tmp[i].first+tmp[i].second] = min(dp[i+1][j+tmp[i].first+tmp[i].second],dp[i][j]+tmp[i].second);
        }
    }
    if(dp[tmp.size()][sum] == inf) {
        cout << -1 << endl;
    }
    else {
        cout << dp[tmp.size()][sum]/2 << endl;
    }
}
0