結果

問題 No.1606 Stuffed Animals Keeper
ユーザー yakkiyakki
提出日時 2021-07-16 22:42:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,951 bytes
コンパイル時間 1,137 ms
コンパイル使用メモリ 123,228 KB
実行使用メモリ 395,856 KB
最終ジャッジ日時 2023-09-20 15:05:05
合計ジャッジ時間 9,715 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
395,460 KB
testcase_01 AC 120 ms
395,660 KB
testcase_02 AC 120 ms
395,504 KB
testcase_03 AC 134 ms
395,496 KB
testcase_04 WA -
testcase_05 AC 125 ms
395,480 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 121 ms
395,556 KB
testcase_11 AC 120 ms
395,520 KB
testcase_12 AC 161 ms
395,480 KB
testcase_13 AC 120 ms
395,548 KB
testcase_14 AC 121 ms
395,764 KB
testcase_15 AC 183 ms
395,628 KB
testcase_16 AC 143 ms
395,580 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 142 ms
395,660 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 122 ms
395,632 KB
testcase_33 WA -
testcase_34 AC 124 ms
395,684 KB
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 122 ms
395,480 KB
testcase_40 AC 119 ms
395,680 KB
testcase_41 AC 121 ms
395,568 KB
testcase_42 AC 121 ms
395,476 KB
testcase_43 AC 142 ms
395,592 KB
testcase_44 AC 143 ms
395,592 KB
testcase_45 AC 143 ms
395,536 KB
testcase_46 AC 142 ms
395,584 KB
testcase_47 AC 142 ms
395,588 KB
testcase_48 AC 119 ms
395,492 KB
testcase_49 AC 121 ms
395,468 KB
testcase_50 AC 121 ms
395,472 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 dp1[5010][5010];
ll dp2[5010][5010];

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,5010){
        dp1[i][j] = LINF;
        dp2[i][j] = LINF;
    }
    dp1[0][0] = 0;
    dp2[0][0] = 0;
    rep(i,m){
        int u = v[i].first;
        int w = v[i].second;
        rep(j,n+1){
            if(dp1[i][j] == LINF) continue;
            dp1[i+1][j+u] = min(dp1[i+1][j+u],dp1[i][j]+u);
            if(j >= w) dp1[i+1][j-w] = min(dp1[i+1][j-w],dp1[i][j]);
        }

        rep(j,n+1){
            if(dp2[i][j] == LINF) continue;
            dp2[i+1][j+w] = min(dp2[i+1][j+w],dp2[i][j]+w);
            if(j >= u) dp2[i+1][j-u] = min(dp2[i+1][j-u],dp2[i][j]);
        }
    }
    if(dp1[m][0] == LINF and dp2[m][0] == LINF) cout << -1 << endl;
    else cout << min(dp1[m][0],dp2[m][0]) << endl;

}

0