結果

問題 No.1606 Stuffed Animals Keeper
ユーザー auauaauaua
提出日時 2021-07-16 23:23:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 121 ms / 3,000 ms
コード長 1,802 bytes
コンパイル時間 1,676 ms
コンパイル使用メモリ 171,692 KB
実行使用メモリ 136,460 KB
最終ジャッジ日時 2023-09-20 15:54:20
合計ジャッジ時間 5,421 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 73 ms
82,280 KB
testcase_04 AC 92 ms
103,376 KB
testcase_05 AC 18 ms
20,832 KB
testcase_06 AC 31 ms
35,580 KB
testcase_07 AC 61 ms
68,536 KB
testcase_08 AC 59 ms
66,376 KB
testcase_09 AC 26 ms
28,480 KB
testcase_10 AC 16 ms
17,804 KB
testcase_11 AC 8 ms
9,308 KB
testcase_12 AC 31 ms
34,600 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 120 ms
134,716 KB
testcase_17 AC 121 ms
136,460 KB
testcase_18 AC 120 ms
135,280 KB
testcase_19 AC 119 ms
134,448 KB
testcase_20 AC 120 ms
135,284 KB
testcase_21 AC 15 ms
16,816 KB
testcase_22 AC 13 ms
14,940 KB
testcase_23 AC 14 ms
15,064 KB
testcase_24 AC 15 ms
16,780 KB
testcase_25 AC 14 ms
15,512 KB
testcase_26 AC 6 ms
6,840 KB
testcase_27 AC 7 ms
6,980 KB
testcase_28 AC 6 ms
6,804 KB
testcase_29 AC 7 ms
7,628 KB
testcase_30 AC 5 ms
6,312 KB
testcase_31 AC 5 ms
5,700 KB
testcase_32 AC 3 ms
4,376 KB
testcase_33 AC 4 ms
5,316 KB
testcase_34 AC 3 ms
4,380 KB
testcase_35 AC 4 ms
4,824 KB
testcase_36 AC 4 ms
5,432 KB
testcase_37 AC 4 ms
4,864 KB
testcase_38 AC 4 ms
4,760 KB
testcase_39 AC 3 ms
4,376 KB
testcase_40 AC 4 ms
4,380 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 3 ms
4,380 KB
testcase_43 AC 116 ms
133,556 KB
testcase_44 AC 117 ms
133,392 KB
testcase_45 AC 116 ms
133,428 KB
testcase_46 AC 116 ms
133,444 KB
testcase_47 AC 116 ms
133,936 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 AC 1 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;
        return 0;
    }
    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