結果

問題 No.1606 Stuffed Animals Keeper
ユーザー auauaauaua
提出日時 2021-07-16 22:19:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,784 bytes
コンパイル時間 1,552 ms
コンパイル使用メモリ 171,012 KB
実行使用メモリ 136,380 KB
最終ジャッジ日時 2023-09-20 14:31:00
合計ジャッジ時間 5,038 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 76 ms
82,260 KB
testcase_04 AC 97 ms
103,552 KB
testcase_05 AC 18 ms
20,556 KB
testcase_06 AC 31 ms
35,596 KB
testcase_07 AC 64 ms
68,380 KB
testcase_08 AC 62 ms
66,588 KB
testcase_09 AC 26 ms
28,448 KB
testcase_10 AC 15 ms
17,836 KB
testcase_11 AC 8 ms
9,260 KB
testcase_12 AC 32 ms
34,388 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 124 ms
134,652 KB
testcase_17 AC 124 ms
136,380 KB
testcase_18 AC 124 ms
135,228 KB
testcase_19 AC 123 ms
134,448 KB
testcase_20 AC 125 ms
135,052 KB
testcase_21 AC 15 ms
16,592 KB
testcase_22 AC 14 ms
14,844 KB
testcase_23 AC 13 ms
14,844 KB
testcase_24 AC 15 ms
16,432 KB
testcase_25 AC 14 ms
15,316 KB
testcase_26 AC 6 ms
6,868 KB
testcase_27 AC 6 ms
7,256 KB
testcase_28 AC 6 ms
6,616 KB
testcase_29 AC 6 ms
7,408 KB
testcase_30 AC 5 ms
6,352 KB
testcase_31 AC 5 ms
5,672 KB
testcase_32 AC 3 ms
4,380 KB
testcase_33 AC 5 ms
5,220 KB
testcase_34 AC 3 ms
4,376 KB
testcase_35 AC 4 ms
4,772 KB
testcase_36 AC 4 ms
4,716 KB
testcase_37 AC 4 ms
4,764 KB
testcase_38 AC 4 ms
4,456 KB
testcase_39 AC 3 ms
4,380 KB
testcase_40 AC 3 ms
4,380 KB
testcase_41 AC 3 ms
4,376 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 121 ms
133,476 KB
testcase_44 AC 121 ms
133,532 KB
testcase_45 AC 121 ms
133,520 KB
testcase_46 AC 121 ms
133,520 KB
testcase_47 AC 121 ms
133,516 KB
testcase_48 WA -
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
//#define vec vector<ll>
//#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=998244353;
const ll inf=INF*INF;
const ll mod=INF;
const ll MAX=1000010;
const double PI=acos(-1.0);
typedef vector<vector<ll> > mat;
typedef vector<ll> vec;





signed main(){
    ll n;cin>>n;
    vector<ll>a(n);
    rep(i,n)cin>>a[i];
    ll cnt=0;
    ll cn=0;
    vector<ll>b(0);
    vector<ll>c(0);
    ll cnt2=0;
    ll cnt0=0;
    ll cnt1=0;
    rep(i,n){
        if(a[i]==2){
            b.pb(cnt);
            c.pb(cn);
            cn=0;
            cnt=0;
            cnt2++;
        }else{
            if(a[i]==0)cn++;
            cnt++;
            if(a[i]==1)cnt1++;
            if(a[i]==0)cnt0++;
        }
    }
    if(cnt1==0||cnt0==0){
        cout<<0<<endl;
        return 0;
    }
    if(cnt2==0){
        cout<<-1<<endl;
    }
    b.pb(cnt);
    c.pb(cn);
    vector<vector<ll> >dp(b.size()+1,vector<ll>(2*n,inf));
    dp[0][n]=0;
    rep(i,b.size()){
        rep(j,2*n){
            if(dp[i][j]==inf)continue;
            ll x=c[i];
            ll y=b[i]-c[i];
            dp[i+1][j+x]=min(dp[i+1][j+x],dp[i][j]+x);
            dp[i+1][j-y]=min(dp[i+1][j-y],dp[i][j]+y);
        }
    }
    if(dp[b.size()][n]==inf){
        cout<<-1<<endl;
    }else{
        cout<<dp[b.size()][n]/2<<endl;
    }
}
0