結果

問題 No.1606 Stuffed Animals Keeper
ユーザー momoyuumomoyuu
提出日時 2023-11-18 04:38:22
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,447 ms / 3,000 ms
コード長 1,603 bytes
コンパイル時間 1,271 ms
コンパイル使用メモリ 110,616 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2023-11-18 04:39:21
合計ジャッジ時間 58,198 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 836 ms
6,676 KB
testcase_04 AC 1,056 ms
6,676 KB
testcase_05 AC 175 ms
6,676 KB
testcase_06 AC 332 ms
6,676 KB
testcase_07 AC 712 ms
6,676 KB
testcase_08 AC 640 ms
6,676 KB
testcase_09 AC 266 ms
6,676 KB
testcase_10 AC 143 ms
6,676 KB
testcase_11 AC 62 ms
6,676 KB
testcase_12 AC 326 ms
6,676 KB
testcase_13 AC 1,430 ms
6,676 KB
testcase_14 AC 1,422 ms
6,676 KB
testcase_15 AC 1,396 ms
6,676 KB
testcase_16 AC 1,428 ms
6,676 KB
testcase_17 AC 1,413 ms
6,676 KB
testcase_18 AC 1,414 ms
6,676 KB
testcase_19 AC 1,414 ms
6,676 KB
testcase_20 AC 1,412 ms
6,676 KB
testcase_21 AC 1,420 ms
6,676 KB
testcase_22 AC 1,435 ms
6,676 KB
testcase_23 AC 1,422 ms
6,676 KB
testcase_24 AC 1,427 ms
6,676 KB
testcase_25 AC 1,426 ms
6,676 KB
testcase_26 AC 1,425 ms
6,676 KB
testcase_27 AC 1,420 ms
6,676 KB
testcase_28 AC 1,422 ms
6,676 KB
testcase_29 AC 1,430 ms
6,676 KB
testcase_30 AC 1,425 ms
6,676 KB
testcase_31 AC 1,429 ms
6,676 KB
testcase_32 AC 1,425 ms
6,676 KB
testcase_33 AC 1,424 ms
6,676 KB
testcase_34 AC 1,426 ms
6,676 KB
testcase_35 AC 1,438 ms
6,676 KB
testcase_36 AC 1,425 ms
6,676 KB
testcase_37 AC 1,422 ms
6,676 KB
testcase_38 AC 1,424 ms
6,676 KB
testcase_39 AC 1,426 ms
6,676 KB
testcase_40 AC 1,429 ms
6,676 KB
testcase_41 AC 1,426 ms
6,676 KB
testcase_42 AC 1,447 ms
6,676 KB
testcase_43 AC 1,416 ms
6,676 KB
testcase_44 AC 1,418 ms
6,676 KB
testcase_45 AC 1,419 ms
6,676 KB
testcase_46 AC 1,416 ms
6,676 KB
testcase_47 AC 1,413 ms
6,676 KB
testcase_48 AC 2 ms
6,676 KB
testcase_49 AC 2 ms
6,676 KB
testcase_50 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<cassert>
#include<set>
using namespace std;
using ll = long long;

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int n;
    cin>>n;
    vector<int> a(n);
    for(int i = 0;i<n;i++) cin>>a[i];
    vector<vector<int>> dp(n+1,vector<int>(3,1e9));
    dp[0][2] = 0;
    for(int i = 0;i<n;i++){
        vector<vector<int>> ndp(n+1,vector<int>(3,1e9));
        if(a[i]==2){
            for(int j = 0;j<=n;j++){
                ndp[j][2] = min(ndp[j][2],dp[j][0]);
                ndp[j][2] = min(ndp[j][2],dp[j][1]);
                ndp[j][2] = min(ndp[j][2],dp[j][2]);
            }
            swap(ndp,dp);
            continue;
        }
        for(int j = 0;j<=n;j++){
            if(a[i]==0){
                if(j+1<=n) ndp[j+1][0] = min(ndp[j+1][0],dp[j][0]);
                if(j+1<=n) ndp[j+1][0] = min(ndp[j+1][0],dp[j][2]);
                ndp[j][1] = min(ndp[j][1],dp[j][1]+1);
                ndp[j][1] = min(ndp[j][1],dp[j][2]+1);
            }else{
                if(j+1<=n) ndp[j+1][0] = min(ndp[j+1][0],dp[j][0]+1);
                if(j+1<=n) ndp[j+1][0] = min(ndp[j+1][0],dp[j][2]+1);
                ndp[j][1] = min(ndp[j][1],dp[j][1]);
                ndp[j][1] = min(ndp[j][1],dp[j][2]);
            }
        }
        swap(ndp,dp);
    }
    int ans = 1e9;
    int cnt = 0;
    for(int i = 0;i<n;i++) if(a[i]==0) cnt++;
    ans = dp[cnt][0];
    ans = min(ans,dp[cnt][1]);
    ans = min(ans,dp[cnt][2]);
    if(ans==1e9) ans = -1;
    else ans = ans / 2;
    cout<<ans<<endl;
}

0