結果

問題 No.1606 Stuffed Animals Keeper
ユーザー sash0sash0
提出日時 2021-07-23 11:07:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 3,000 ms
コード長 3,117 bytes
コンパイル時間 3,049 ms
コンパイル使用メモリ 226,552 KB
実行使用メモリ 35,788 KB
最終ジャッジ日時 2023-09-25 09:10:36
合計ジャッジ時間 6,212 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 20 ms
16,532 KB
testcase_04 AC 23 ms
19,932 KB
testcase_05 AC 5 ms
5,892 KB
testcase_06 AC 9 ms
8,552 KB
testcase_07 AC 17 ms
14,344 KB
testcase_08 AC 16 ms
13,328 KB
testcase_09 AC 8 ms
7,584 KB
testcase_10 AC 5 ms
5,416 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 9 ms
8,644 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 30 ms
24,684 KB
testcase_17 AC 30 ms
25,104 KB
testcase_18 AC 31 ms
25,004 KB
testcase_19 AC 30 ms
25,008 KB
testcase_20 AC 29 ms
24,664 KB
testcase_21 AC 6 ms
6,464 KB
testcase_22 AC 6 ms
5,892 KB
testcase_23 AC 6 ms
5,860 KB
testcase_24 AC 7 ms
6,396 KB
testcase_25 AC 6 ms
6,132 KB
testcase_26 AC 4 ms
4,376 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 4 ms
4,376 KB
testcase_29 AC 3 ms
4,380 KB
testcase_30 AC 3 ms
4,376 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 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 2 ms
4,380 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 3 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 41 ms
35,708 KB
testcase_44 AC 41 ms
35,712 KB
testcase_45 AC 41 ms
35,788 KB
testcase_46 AC 42 ms
35,720 KB
testcase_47 AC 42 ms
35,696 KB
testcase_48 AC 2 ms
4,376 KB
testcase_49 AC 2 ms
4,380 KB
testcase_50 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// clang-format off
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcountll
#define ffs __builtin_ffsll
#define ctz __builtin_ctzll
#define clz __builtin_clz
#define clzll __builtin_clzll
#define all(a) (a).begin(), (a).end()

using namespace std;
using namespace __gnu_pbds;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
template <typename T> bool getf(T flag, size_t i) { return (flag>>i) & 1; }
template <typename T> T setf(T flag, size_t i) { return flag | (T(1)<<i); }
template <typename T> T unsetf(T flag, size_t i) { return flag & ~(T(1)<<i); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
// clang-format on

int main() {
    fastio();

    int n;
    cin >> n;
    vi cnt0, cnt1;
    int c0 = 0, c1 = 0;
    forn(i, n) {
        int x;
        cin >> x;
        if (x == 0)
            c0++;
        else if (x == 1)
            c1++;
        else {
            if (c0 + c1 > 0) {
                cnt0.push_back(c0);
                cnt1.push_back(c1);
            }
            c0 = 0;
            c1 = 0;
        }
    }
    if (c0 + c1 > 0) {
        cnt0.push_back(c0);
        cnt1.push_back(c1);
    }

    const int INF = 11111;
    int base = accumulate(all(cnt1), 0);

    int m = cnt0.size();

    vec<vi> dp(m + 1, vi(n + 1, INF)); // dp[i][j]: i番目までの収支がjのときの最小操作回数
    dp[0][base] = 0;

    forn1(i, m) {
        forn(j, n + 1) {
            if (dp[i - 1][j] == INF) continue;

            // i番目のグループをすべて肉食動物にする
            int k1 = j - cnt1[i - 1];
            chmin(dp[i][k1], dp[i - 1][j] + cnt1[i - 1]);

            // i番目のグループをすべて草食動物にする
            int k2 = j + cnt0[i - 1];
            chmin(dp[i][k2], dp[i - 1][j] + cnt0[i - 1]);
        }
    }

    int ans = 0;
    if (dp[m][base] == INF) {
        ans = -1;
    } else {
        ans = dp[m][base] / 2;
    }

    cout << ans << endl;

    return 0;
}
0