結果

問題 No.1606 Stuffed Animals Keeper
ユーザー SSRSSSRS
提出日時 2021-07-16 21:34:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 18 ms / 3,000 ms
コード長 929 bytes
コンパイル時間 2,059 ms
コンパイル使用メモリ 170,956 KB
実行使用メモリ 14,432 KB
最終ジャッジ日時 2023-09-20 13:14:36
合計ジャッジ時間 3,887 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 11 ms
9,604 KB
testcase_04 AC 14 ms
11,636 KB
testcase_05 AC 4 ms
4,496 KB
testcase_06 AC 6 ms
5,984 KB
testcase_07 AC 10 ms
8,864 KB
testcase_08 AC 9 ms
8,192 KB
testcase_09 AC 5 ms
5,296 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 5 ms
5,604 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 18 ms
13,816 KB
testcase_17 AC 17 ms
14,164 KB
testcase_18 AC 18 ms
14,432 KB
testcase_19 AC 17 ms
14,140 KB
testcase_20 AC 17 ms
13,828 KB
testcase_21 AC 4 ms
4,504 KB
testcase_22 AC 4 ms
4,624 KB
testcase_23 AC 4 ms
4,496 KB
testcase_24 AC 5 ms
4,588 KB
testcase_25 AC 4 ms
4,840 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 3 ms
4,380 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 3 ms
4,380 KB
testcase_34 AC 3 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 3 ms
4,376 KB
testcase_37 AC 3 ms
4,380 KB
testcase_38 AC 3 ms
4,376 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 3 ms
4,376 KB
testcase_41 AC 3 ms
4,376 KB
testcase_42 AC 3 ms
4,376 KB
testcase_43 AC 18 ms
14,312 KB
testcase_44 AC 17 ms
13,840 KB
testcase_45 AC 18 ms
13,736 KB
testcase_46 AC 18 ms
13,828 KB
testcase_47 AC 18 ms
14,052 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 10000000;
int main(){
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  vector<int> cnt0(1, 0), cnt1(1, 0);
  int s0 = 0;
  int cnt = 1;
  for (int i = 0; i < N; i++){
    if (A[i] == 0){
      cnt0.back()++;
      s0++;
    }
    if (A[i] == 1){
      cnt1.back()++;
    }
    if (A[i] == 2){
      cnt0.push_back(0);
      cnt1.push_back(0);
      cnt++;
    }
  }
  vector<vector<int>> dp(cnt + 1, vector<int>(s0 + 1, INF));
  dp[0][0] = 0;
  for (int i = 0; i < cnt; i++){
    for (int j = 0; j <= s0; j++){
      if (j + cnt0[i] + cnt1[i] <= s0){
        dp[i + 1][j + cnt0[i] + cnt1[i]] = min(dp[i + 1][j + cnt0[i] + cnt1[i]], dp[i][j] + cnt1[i]);
      }
      dp[i + 1][j] = min(dp[i + 1][j], dp[i][j]);
    }
  }
  if (dp[cnt][s0] == INF){
    cout << -1 << endl;
  } else {
    cout << dp[cnt][s0] << endl;
  }
}
0