結果

問題 No.1606 Stuffed Animals Keeper
ユーザー 👑 amentorimaruamentorimaru
提出日時 2023-03-02 00:56:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 66 ms / 3,000 ms
コード長 1,131 bytes
コンパイル時間 1,485 ms
コンパイル使用メモリ 122,948 KB
実行使用メモリ 47,400 KB
最終ジャッジ日時 2023-10-16 22:12:33
合計ジャッジ時間 5,189 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 35 ms
29,712 KB
testcase_04 AC 45 ms
36,576 KB
testcase_05 AC 10 ms
9,120 KB
testcase_06 AC 16 ms
14,136 KB
testcase_07 AC 30 ms
25,752 KB
testcase_08 AC 30 ms
24,432 KB
testcase_09 AC 13 ms
12,024 KB
testcase_10 AC 8 ms
8,064 KB
testcase_11 AC 5 ms
5,424 KB
testcase_12 AC 16 ms
13,872 KB
testcase_13 AC 3 ms
4,348 KB
testcase_14 AC 3 ms
4,348 KB
testcase_15 AC 4 ms
4,348 KB
testcase_16 AC 56 ms
47,136 KB
testcase_17 AC 66 ms
47,400 KB
testcase_18 AC 60 ms
47,136 KB
testcase_19 AC 58 ms
47,136 KB
testcase_20 AC 59 ms
47,136 KB
testcase_21 AC 11 ms
9,912 KB
testcase_22 AC 10 ms
9,120 KB
testcase_23 AC 10 ms
9,120 KB
testcase_24 AC 11 ms
9,912 KB
testcase_25 AC 10 ms
9,384 KB
testcase_26 AC 5 ms
5,160 KB
testcase_27 AC 5 ms
5,424 KB
testcase_28 AC 5 ms
5,160 KB
testcase_29 AC 6 ms
5,424 KB
testcase_30 AC 4 ms
4,896 KB
testcase_31 AC 4 ms
4,632 KB
testcase_32 AC 3 ms
4,348 KB
testcase_33 AC 4 ms
4,368 KB
testcase_34 AC 3 ms
4,348 KB
testcase_35 AC 4 ms
4,368 KB
testcase_36 AC 4 ms
4,368 KB
testcase_37 AC 4 ms
4,368 KB
testcase_38 AC 4 ms
4,348 KB
testcase_39 AC 3 ms
4,348 KB
testcase_40 AC 3 ms
4,348 KB
testcase_41 AC 3 ms
4,348 KB
testcase_42 AC 4 ms
4,348 KB
testcase_43 AC 56 ms
46,872 KB
testcase_44 AC 56 ms
46,872 KB
testcase_45 AC 57 ms
46,872 KB
testcase_46 AC 57 ms
46,872 KB
testcase_47 AC 56 ms
46,872 KB
testcase_48 AC 2 ms
4,348 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<unordered_set>
#include<unordered_map>
#include<cmath>
#include <algorithm>
#include <random>
using namespace std;
using ll = long long;

#define REP(i, n) for(ll i=0; i<(n);i++)
#define VL vector<ll>

std::random_device seed_gen;
std::default_random_engine eg(seed_gen());


int main() {
  ll n;
  cin >> n;
  VL a(n);
  REP(i, n)
    cin >> a[i];
  VL z, o;
  ll zc = 0, oc = 0;
  z.push_back(0);
  o.push_back(0);
  REP(i, n) {
    if (a[i] == 2) {
      z.push_back(0);
      o.push_back(0);
    }
    else if (a[i]) {
      o.back()++;
      oc++;
    }
    else {
      z.back()++;
      zc++;
    }
  }
  vector dp(z.size() + 1, vector<ll>(zc + oc + 1, 1e18));
  dp[0][zc] = 0;
  REP(i, z.size()) {
    REP(j, zc+oc+1) {
      if (j + o[i] < zc + oc + 1) {        
        dp[i + 1][j + o[i]] = min(dp[i + 1][j + o[i]], dp[i][j] + z[i]);
      }
      if (j - z[i] >= 0) {
        dp[i + 1][j - z[i]] = min(dp[i + 1][j - z[i]], dp[i][j] + o[i]);
      }
    }
  }
  if (dp[z.size()][oc] > 1e17) {
    cout << -1;
  }
  else {
    cout << dp[z.size()][oc] / 2;
  }

  return 0;
}
0