結果

問題 No.1606 Stuffed Animals Keeper
ユーザー inksamuraiinksamurai
提出日時 2021-07-16 23:27:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,864 bytes
コンパイル時間 1,038 ms
コンパイル使用メモリ 109,224 KB
実行使用メモリ 14,464 KB
最終ジャッジ日時 2023-09-20 15:58:39
合計ジャッジ時間 2,995 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
4,376 KB
testcase_14 WA -
testcase_15 AC 2 ms
4,376 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 6 ms
6,240 KB
testcase_22 WA -
testcase_23 AC 5 ms
5,908 KB
testcase_24 AC 5 ms
6,244 KB
testcase_25 WA -
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 4 ms
4,380 KB
testcase_30 AC 3 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 3 ms
4,380 KB
testcase_36 WA -
testcase_37 AC 3 ms
4,376 KB
testcase_38 WA -
testcase_39 AC 3 ms
4,380 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,376 KB
testcase_42 AC 3 ms
4,376 KB
testcase_43 AC 13 ms
14,048 KB
testcase_44 AC 13 ms
14,464 KB
testcase_45 AC 13 ms
13,796 KB
testcase_46 AC 13 ms
14,084 KB
testcase_47 AC 13 ms
14,208 KB
testcase_48 AC 1 ms
4,376 KB
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <tuple>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <functional>
#include <numeric>
#include <iomanip> 
#include <stdio.h>
#include <assert.h>
//eolibraries
#define lnf 3999999999999999999
#define inf 3999999999999999999
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define all(c) (c).begin(),(c).end()
#define sz(c) (ll)(c).size()
#define make_unique(a) sort(all(a)),a.erase(unique(all(a)),a.end())
#define pii pair <ll,ll>
#define rep(i,n) for(ll i = 0 ; i < n ; i++) 
#define drep(i,n) for(ll i = n-1 ; i >= 0 ; i--)
#define crep(i,x,n) for(ll i = x ; i < n ; i++)
#define vi vector <ll> 
#define vec(...) vector<__VA_ARGS__>
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
//eodefine
using namespace std;

const ll mxn=500000;

int main(){
fcin;
	ll n;
	cin>>n;
	vi a(n);
	rep(i,n) cin>>a[i];
	ll e=0,w=0;
	rep(i,n){
		if(a[i]==1) e++;
		else if(a[i]==0) w++;
	}
	vec(pii) nea;
	for(ll i=0;i<n;){
		ll cnt1=0,cnt0=0;
		while(i<n and a[i]!=2){
			if(a[i]==1) cnt1++;
			else if(a[i]==0) cnt0++;
			i=i+1;
		}
		if(cnt1 and cnt0) nea.pb({cnt1,cnt0});
		while(a[i]==2) i++;
	}
	vec(vi) dp(sz(nea)+2,vi(e+3,inf));
	dp[0][e]=0;
	ll cnt0=0,cnt1=0;
	rep(i,sz(nea)){
		pii p=nea[i];
		drep(j,e+1){
			if(dp[i][j]==inf) continue;
			if(p.fi+p.se<=j){
				dp[i+1][j-p.fi-p.se]=min(dp[i+1][j-p.fi-p.se],dp[i][j]+p.se);
			}
			if(p.fi+p.se<=w-(cnt1+cnt0-(e-j))){
				dp[i+1][j]=min(dp[i+1][j],dp[i][j]+p.fi);
			}
		}
		cnt1+=p.fi; cnt0+=p.se;
	}
	ll ans=dp[sz(nea)][0];
	if(ans==inf) cout<<"-1\n";
	else cout<<ans/2<<"\n";

//
	return 0;
}
0