結果

問題 No.1606 Stuffed Animals Keeper
ユーザー milanis48663220milanis48663220
提出日時 2021-07-16 22:15:50
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 71 ms / 3,000 ms
コード長 2,397 bytes
コンパイル時間 1,205 ms
コンパイル使用メモリ 122,028 KB
実行使用メモリ 68,580 KB
最終ジャッジ日時 2023-09-20 14:24:16
合計ジャッジ時間 3,826 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 32 ms
29,512 KB
testcase_04 AC 39 ms
36,632 KB
testcase_05 AC 8 ms
8,856 KB
testcase_06 AC 14 ms
14,052 KB
testcase_07 AC 28 ms
25,752 KB
testcase_08 AC 25 ms
23,696 KB
testcase_09 AC 12 ms
11,668 KB
testcase_10 AC 7 ms
7,796 KB
testcase_11 AC 4 ms
5,004 KB
testcase_12 AC 15 ms
14,040 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 50 ms
45,900 KB
testcase_17 AC 51 ms
46,980 KB
testcase_18 AC 52 ms
46,776 KB
testcase_19 AC 51 ms
46,488 KB
testcase_20 AC 50 ms
46,456 KB
testcase_21 AC 10 ms
9,708 KB
testcase_22 AC 8 ms
9,000 KB
testcase_23 AC 8 ms
9,028 KB
testcase_24 AC 9 ms
9,820 KB
testcase_25 AC 9 ms
9,292 KB
testcase_26 AC 4 ms
4,796 KB
testcase_27 AC 4 ms
5,192 KB
testcase_28 AC 4 ms
4,956 KB
testcase_29 AC 5 ms
4,984 KB
testcase_30 AC 4 ms
4,592 KB
testcase_31 AC 4 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 3 ms
4,376 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 3 ms
4,380 KB
testcase_36 AC 3 ms
4,376 KB
testcase_37 AC 3 ms
4,376 KB
testcase_38 AC 3 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 70 ms
68,472 KB
testcase_44 AC 70 ms
68,336 KB
testcase_45 AC 70 ms
68,336 KB
testcase_46 AC 71 ms
68,456 KB
testcase_47 AC 70 ms
68,580 KB
testcase_48 AC 1 ms
4,380 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;


template <typename T>
class OffsetVec{
    public:
	int n;
    vector<T> v;
	T& operator[](int x) {
        return v[x+n];
	}
    void print(){
        for(int i = -n; i <= n; i++) cout << v[i+n] << ' ';
        cout << endl;
    }
    OffsetVec(int _n){
        n = _n;
        v = vector<T>(2*n+1);
    }
    OffsetVec(int _n, T x){
        n = _n;
        v = vector<T>(2*n+1, x);
    }
};

const int INF = 1e9;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n; cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    vector<int> s, t;
    vector<int> cnt(2);
    auto add = [&](){
        if(cnt[0]+cnt[1] > 0){
            s.push_back(cnt[0]);
            t.push_back(cnt[1]);
        }
    };
    for(int i = 0; i < n; i++){
        if(a[i] == 2){
            add();
            cnt[0] = 0;      
            cnt[1] = 0;      
        }else{
            cnt[a[i]]++;
        }
    }
    add();
    int m = s.size();
    vector<OffsetVec<int>> dp(m+1, OffsetVec<int>(n+1, INF));
    dp[0][0] = 0;
    for(int i = 0; i < m; i++){
        // cout << s[i] << ' ' << t[i] << endl;
        for(int j = -n; j <= n; j++){
            if(dp[i][j] == INF) continue;
            // 0の場所にする
            if(j < 0){
                chmin(dp[i+1][j+t[i]], dp[i][j]+min(-j, t[i]));
            }else{
                chmin(dp[i+1][j+t[i]], dp[i][j]);
            }
            // 1の場所にする
            if(j < 0){
                chmin(dp[i+1][j-s[i]], dp[i][j]);
            }else{
                chmin(dp[i+1][j-s[i]], dp[i][j]+min(j, s[i]));
            }
        }
    }
    if(dp[m][0] == INF) cout << -1 << endl;
    else cout << dp[m][0] << endl;
}
0