結果

問題 No.1606 Stuffed Animals Keeper
ユーザー umezoumezo
提出日時 2021-07-17 03:03:16
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 57 ms / 3,000 ms
コード長 978 bytes
コンパイル時間 2,273 ms
コンパイル使用メモリ 202,196 KB
実行使用メモリ 101,676 KB
最終ジャッジ日時 2023-09-20 19:16:54
合計ジャッジ時間 7,052 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
101,448 KB
testcase_01 AC 49 ms
101,456 KB
testcase_02 AC 49 ms
101,548 KB
testcase_03 AC 53 ms
101,488 KB
testcase_04 AC 53 ms
101,424 KB
testcase_05 AC 50 ms
101,376 KB
testcase_06 AC 51 ms
101,428 KB
testcase_07 AC 52 ms
101,472 KB
testcase_08 AC 52 ms
101,552 KB
testcase_09 AC 51 ms
101,572 KB
testcase_10 AC 50 ms
101,564 KB
testcase_11 AC 49 ms
101,484 KB
testcase_12 AC 50 ms
101,616 KB
testcase_13 AC 51 ms
101,400 KB
testcase_14 AC 50 ms
101,468 KB
testcase_15 AC 50 ms
101,420 KB
testcase_16 AC 54 ms
101,488 KB
testcase_17 AC 54 ms
101,444 KB
testcase_18 AC 54 ms
101,428 KB
testcase_19 AC 53 ms
101,432 KB
testcase_20 AC 53 ms
101,448 KB
testcase_21 AC 50 ms
101,420 KB
testcase_22 AC 50 ms
101,488 KB
testcase_23 AC 50 ms
101,416 KB
testcase_24 AC 51 ms
101,416 KB
testcase_25 AC 51 ms
101,404 KB
testcase_26 AC 51 ms
101,480 KB
testcase_27 AC 50 ms
101,588 KB
testcase_28 AC 50 ms
101,504 KB
testcase_29 AC 51 ms
101,536 KB
testcase_30 AC 50 ms
101,468 KB
testcase_31 AC 50 ms
101,424 KB
testcase_32 AC 51 ms
101,476 KB
testcase_33 AC 51 ms
101,392 KB
testcase_34 AC 50 ms
101,412 KB
testcase_35 AC 50 ms
101,396 KB
testcase_36 AC 50 ms
101,396 KB
testcase_37 AC 51 ms
101,416 KB
testcase_38 AC 51 ms
101,660 KB
testcase_39 AC 50 ms
101,400 KB
testcase_40 AC 51 ms
101,408 KB
testcase_41 AC 49 ms
101,400 KB
testcase_42 AC 52 ms
101,476 KB
testcase_43 AC 55 ms
101,408 KB
testcase_44 AC 56 ms
101,572 KB
testcase_45 AC 55 ms
101,424 KB
testcase_46 AC 57 ms
101,432 KB
testcase_47 AC 56 ms
101,424 KB
testcase_48 AC 50 ms
101,676 KB
testcase_49 AC 49 ms
101,400 KB
testcase_50 AC 49 ms
101,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

int dp[5010][5010];
const int INF=1e9+7;

int main(){
  int n;
  cin>>n;
  
  vector<int> A(n+1);
  rep(i,n) cin>>A[i];
  A[n]=2;
  
  vector<int> B,C,D;
  int cnt0=0,cnt1=0,sum0=0;
  rep(i,n+1){
    if(A[i]==0){
      cnt0++;
      sum0++;
    }
    else if(A[i]==1) cnt1++;
    else{
      if(cnt0==0 && cnt1==0) continue;
      else{
        B.push_back(cnt0);
        C.push_back(cnt1);
        D.push_back(cnt0+cnt1);
        cnt0=0,cnt1=0;
      }
    }
  }
  
  rep(i,5010) rep(j,5010) dp[i][j]=INF;
  dp[0][sum0]=0;
  int m=B.size();
  rep(i,m){
    rep(j,sum0+1){
      if(dp[i][j]==INF) continue;
      dp[i+1][j]=min(dp[i+1][j],dp[i][j]+B[i]);
      if(j-D[i]>=0) dp[i+1][j-D[i]]=min(dp[i+1][j-D[i]],dp[i][j]+C[i]);
    }
  }
  
  if(dp[m][0]==INF) cout<<-1<<endl;
  else cout<<dp[m][0]/2<<endl;

  return 0;
}
0