結果

問題 No.1606 Stuffed Animals Keeper
ユーザー outlineoutline
提出日時 2021-08-01 18:23:28
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 204 ms / 3,000 ms
コード長 2,325 bytes
コンパイル時間 1,358 ms
コンパイル使用メモリ 135,272 KB
実行使用メモリ 296,896 KB
最終ジャッジ日時 2023-10-14 18:59:46
合計ジャッジ時間 10,729 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 120 ms
206,996 KB
testcase_04 AC 153 ms
247,376 KB
testcase_05 AC 35 ms
90,936 KB
testcase_06 AC 53 ms
115,264 KB
testcase_07 AC 103 ms
188,316 KB
testcase_08 AC 93 ms
175,816 KB
testcase_09 AC 45 ms
106,208 KB
testcase_10 AC 31 ms
84,444 KB
testcase_11 AC 18 ms
65,416 KB
testcase_12 AC 51 ms
114,332 KB
testcase_13 AC 195 ms
296,760 KB
testcase_14 AC 196 ms
296,748 KB
testcase_15 AC 202 ms
296,704 KB
testcase_16 AC 187 ms
296,828 KB
testcase_17 AC 185 ms
296,700 KB
testcase_18 AC 184 ms
296,684 KB
testcase_19 AC 185 ms
296,712 KB
testcase_20 AC 183 ms
296,780 KB
testcase_21 AC 173 ms
296,684 KB
testcase_22 AC 171 ms
296,692 KB
testcase_23 AC 172 ms
296,696 KB
testcase_24 AC 175 ms
296,840 KB
testcase_25 AC 173 ms
296,760 KB
testcase_26 AC 176 ms
296,780 KB
testcase_27 AC 174 ms
296,840 KB
testcase_28 AC 174 ms
296,896 KB
testcase_29 AC 175 ms
296,696 KB
testcase_30 AC 178 ms
296,796 KB
testcase_31 AC 204 ms
296,712 KB
testcase_32 AC 185 ms
296,712 KB
testcase_33 AC 191 ms
296,720 KB
testcase_34 AC 186 ms
296,820 KB
testcase_35 AC 176 ms
296,868 KB
testcase_36 AC 195 ms
296,692 KB
testcase_37 AC 186 ms
296,696 KB
testcase_38 AC 204 ms
296,760 KB
testcase_39 AC 183 ms
296,692 KB
testcase_40 AC 187 ms
296,712 KB
testcase_41 AC 182 ms
296,696 KB
testcase_42 AC 182 ms
296,712 KB
testcase_43 AC 185 ms
296,712 KB
testcase_44 AC 186 ms
296,708 KB
testcase_45 AC 188 ms
296,700 KB
testcase_46 AC 186 ms
296,776 KB
testcase_47 AC 184 ms
296,840 KB
testcase_48 AC 2 ms
4,352 KB
testcase_49 AC 2 ms
4,348 KB
testcase_50 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
// #pragma GCC target("avx512f,avx512dq,avx512cd,avx512bw,avx512vl")

using ll = long long;
constexpr int INF = 1001001001;
// constexpr int mod = 1000000007;
constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

int dp[5005][5005][3];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, zero = 0;
    cin >> N;
    vector<int> A(N);
    for(int i = 0; i < N; ++i){
        cin >> A[i];
        zero += A[i] == 0;
    }

    for(int i = 0; i <= N; ++i){
        for(int j = 0; j <= N; ++j){
            for(int k = 0; k <= 2; ++k){
                dp[i][j][k] = INF;
            }
        }
    }

    dp[0][0][2] = 0;
    for(int i = 0; i < N; ++i){
        if(A[i] == 2){
            for(int j = 0; j <= i; ++j){
                for(int k = 0; k <= 2; ++k){
                    chmin(dp[i + 1][j][2], dp[i][j][k]);
                }
            }
        }
        else{
            for(int j = 0; j <= i; ++j){
                chmin(dp[i + 1][j + 1][0], dp[i][j][0] + (A[i] == 1));
                chmin(dp[i + 1][j][1], dp[i][j][1] + (A[i] == 0));
                chmin(dp[i + 1][j + 1][0], dp[i][j][2] + (A[i] == 1));
                chmin(dp[i + 1][j][1], dp[i][j][2] + (A[i] == 0));
            }
        }
    }

    int ans = min({dp[N][zero][0], dp[N][zero][1], dp[N][zero][2]});
    if(ans == INF)  ans = -1;
    else    ans /= 2;

    cout << ans << endl;

    return 0;
}
0