結果

問題 No.1606 Stuffed Animals Keeper
ユーザー snow39snow39
提出日時 2021-07-16 22:05:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,938 bytes
コンパイル時間 957 ms
コンパイル使用メモリ 110,100 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-20 14:05:38
合計ジャッジ時間 2,831 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 9 ms
4,376 KB
testcase_04 AC 11 ms
4,380 KB
testcase_05 AC 3 ms
4,376 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 WA -
testcase_08 AC 8 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 5 ms
4,380 KB
testcase_13 WA -
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 13 ms
4,376 KB
testcase_17 WA -
testcase_18 AC 14 ms
4,376 KB
testcase_19 AC 13 ms
4,376 KB
testcase_20 AC 14 ms
4,380 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 WA -
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 4 ms
4,376 KB
testcase_25 AC 3 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 2 ms
4,384 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 WA -
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 2 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 WA -
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,380 KB
testcase_41 WA -
testcase_42 WA -
testcase_43 AC 17 ms
4,376 KB
testcase_44 AC 17 ms
4,380 KB
testcase_45 AC 17 ms
4,380 KB
testcase_46 AC 17 ms
4,376 KB
testcase_47 AC 17 ms
4,376 KB
testcase_48 AC 1 ms
4,376 KB
testcase_49 AC 1 ms
4,376 KB
testcase_50 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘void solve()’ 内:
main.cpp:53:5: 警告: ‘cnt_one’ may be used uninitialized [-Wmaybe-uninitialized]
   53 |     if (cnt_zero + cnt_one > 0) {
      |     ^~
main.cpp:38:23: 備考: ‘cnt_one’ はここで定義されています
   38 |     int cnt_zero = 0, cnt_one;
      |                       ^~~~~~~

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

#define mkp make_pair
#define mkt make_tuple
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
template <class T>
void chmin(T &a, const T &b) {
    if (a > b) a = b;
}
template <class T>
void chmax(T &a, const T &b) {
    if (a < b) a = b;
}

const int INF = 1e9;

void solve() {
    int N;
    cin >> N;
    vector<int> A(N);
    rep(i, N) cin >> A[i];

    vector<pair<int, int>> B;
    int cnt_zero = 0, cnt_one;
    rep(i, N) {
        if (A[i] == 2) {
            if (cnt_zero + cnt_one > 0) {
                B.push_back(mkp(cnt_zero, cnt_one));
                cnt_zero = 0;
                cnt_one = 0;
            }
        } else {
            if (A[i] == 0)
                cnt_zero++;
            else
                cnt_one++;
        }
    }
    if (cnt_zero + cnt_one > 0) {
        B.push_back(mkp(cnt_zero, cnt_one));
        cnt_zero = 0;
        cnt_one = 0;
    }

    int V = B.size();
    vector<int> dp(N + 1, INF);
    dp[0] = 0;
    rep(i, V) {
        auto [cz, co] = B[i];
        int cnt = cz + co;
        vector<int> ndp(N + 1, INF);
        for (int j = 0; j <= N; j++) {
            if (dp[j] == INF) continue;

            chmin(ndp[j], dp[j]);
            if (j + cnt <= N) chmin(ndp[j + cnt], dp[j] + co);
        }
        swap(dp, ndp);
    }

    int zero = 0, one = 0;
    rep(i, N) {
        if (A[i] == 0)
            zero++;
        else if (A[i] == 1)
            one++;
    }

    if (dp[zero] != INF) {
        cout << dp[zero] << endl;
    } else {
        cout << -1 << endl;
    }
}

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

    solve();

    return 0;
}
0