結果

問題 No.1606 Stuffed Animals Keeper
ユーザー yakkiyakki
提出日時 2021-07-16 22:56:18
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 229 ms / 3,000 ms
コード長 1,635 bytes
コンパイル時間 1,160 ms
コンパイル使用メモリ 122,872 KB
実行使用メモリ 395,444 KB
最終ジャッジ日時 2023-09-20 15:22:32
合計ジャッジ時間 12,678 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 182 ms
395,104 KB
testcase_01 AC 215 ms
395,112 KB
testcase_02 AC 176 ms
395,188 KB
testcase_03 AC 193 ms
395,160 KB
testcase_04 AC 195 ms
395,232 KB
testcase_05 AC 182 ms
395,156 KB
testcase_06 AC 215 ms
395,236 KB
testcase_07 AC 187 ms
395,444 KB
testcase_08 AC 186 ms
395,320 KB
testcase_09 AC 183 ms
395,136 KB
testcase_10 AC 180 ms
395,196 KB
testcase_11 AC 177 ms
395,180 KB
testcase_12 AC 183 ms
395,188 KB
testcase_13 AC 173 ms
395,160 KB
testcase_14 AC 175 ms
395,160 KB
testcase_15 AC 229 ms
395,256 KB
testcase_16 AC 193 ms
395,172 KB
testcase_17 AC 194 ms
395,160 KB
testcase_18 AC 196 ms
395,148 KB
testcase_19 AC 193 ms
395,172 KB
testcase_20 AC 190 ms
395,160 KB
testcase_21 AC 176 ms
395,156 KB
testcase_22 AC 171 ms
395,136 KB
testcase_23 AC 175 ms
395,152 KB
testcase_24 AC 174 ms
395,200 KB
testcase_25 AC 174 ms
395,216 KB
testcase_26 AC 172 ms
395,152 KB
testcase_27 AC 173 ms
395,136 KB
testcase_28 AC 172 ms
395,272 KB
testcase_29 AC 172 ms
395,264 KB
testcase_30 AC 170 ms
395,144 KB
testcase_31 AC 171 ms
395,344 KB
testcase_32 AC 171 ms
395,180 KB
testcase_33 AC 174 ms
395,296 KB
testcase_34 AC 173 ms
395,144 KB
testcase_35 AC 173 ms
395,132 KB
testcase_36 AC 171 ms
395,132 KB
testcase_37 AC 169 ms
395,272 KB
testcase_38 AC 169 ms
395,160 KB
testcase_39 AC 170 ms
395,208 KB
testcase_40 AC 168 ms
395,300 KB
testcase_41 AC 170 ms
395,276 KB
testcase_42 AC 170 ms
395,132 KB
testcase_43 AC 190 ms
395,184 KB
testcase_44 AC 190 ms
395,160 KB
testcase_45 AC 190 ms
395,236 KB
testcase_46 AC 188 ms
395,368 KB
testcase_47 AC 191 ms
395,292 KB
testcase_48 AC 169 ms
395,172 KB
testcase_49 AC 225 ms
395,108 KB
testcase_50 AC 170 ms
395,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<bitset>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<iomanip>
#include<cmath>
#include<cstring>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<numeric>
#include<ctime>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
//#define MOD 1000000007
#define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592
#define PI acos(-1.0)

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int,int>;
using Pl = pair<ll,ll>;
//using mint = modint998244353;

int dh[] = {1,0,-1,0};
int dw[] = {0,1,0,-1};

ll dp[5010][10010];

int main(){
    int n; cin >> n;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    int cnt0 = 0,cnt1 = 0;
    vector<Pi> v;
    rep(i,n){
        if(a[i] == 2){
            v.emplace_back(cnt0,cnt1);
            cnt0 = 0;
            cnt1 = 0;
        }
        else if(a[i] == 0) cnt0++;
        else cnt1++;
    }
    v.emplace_back(cnt0,cnt1);

    int m = v.size();
    rep(i,5010)rep(j,10010){
        dp[i][j] = LINF;
    }
    dp[0][5000] = 0;
    rep(i,m){
        int u = v[i].first;
        int w = v[i].second;
        rep(j,10010){
            if(dp[i][j] == LINF) continue;
            dp[i+1][j+u] = min(dp[i+1][j+u],dp[i][j]+u);
            dp[i+1][j-w] = min(dp[i+1][j-w],dp[i][j]);
        }
    }
    if(dp[m][5000] == LINF) cout << -1 << endl;
    else cout << dp[m][5000] << endl;

}

0