結果

問題 No.1606 Stuffed Animals Keeper
ユーザー 👑 Nachia
提出日時 2021-07-16 22:34:02
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 37 ms / 3,000 ms
コード長 925 bytes
コンパイル時間 693 ms
コンパイル使用メモリ 78,032 KB
最終ジャッジ日時 2025-01-23 02:22:08
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #

// Nachia くんだよ

#include <iostream>
#include <vector>
#include <deque>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

int N;
vector<int> A;
vector<pair<int,int>> WV;
vector<int> dp;

int main() {
  cin >> N;
  A.resize(N);
  rep(i,N) cin >> A[i];
  WV.push_back({0,0});
  int tgSum = 0;
  rep(i,N){
    if(A[i] == 0 || A[i] == 1) WV.back().first++;
    if(A[i] == 0) tgSum++;
    if(A[i] == 1) WV.back().second++;
    if(A[i] == 2) WV.push_back({0,0});
  }

  dp.assign(tgSum+1,1001001);
  dp[0] = 0;
  for(auto wv : WV){
    for(int k=tgSum; k>=wv.first; k--){
      dp[k] = min(dp[k],dp[k-wv.first]+wv.second);
    }
  }

  if(dp[tgSum] >= N) cout << "-1\n";
  else cout << dp[tgSum] << "\n";
  return 0;
}


struct ios_do_not_sync{
  ios_do_not_sync(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} ios_do_not_sync_instance;


0