結果

問題 No.1606 Stuffed Animals Keeper
ユーザー trineutrontrineutron
提出日時 2021-07-16 22:22:07
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 3,000 ms
コード長 1,174 bytes
コンパイル時間 2,138 ms
コンパイル使用メモリ 206,672 KB
実行使用メモリ 14,200 KB
最終ジャッジ日時 2023-09-20 14:35:17
合計ジャッジ時間 4,001 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 15 ms
9,472 KB
testcase_04 AC 19 ms
11,376 KB
testcase_05 AC 5 ms
4,940 KB
testcase_06 AC 7 ms
5,748 KB
testcase_07 AC 13 ms
8,604 KB
testcase_08 AC 13 ms
8,580 KB
testcase_09 AC 7 ms
5,148 KB
testcase_10 AC 5 ms
4,452 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 8 ms
5,564 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 25 ms
13,988 KB
testcase_17 AC 25 ms
13,960 KB
testcase_18 AC 24 ms
13,824 KB
testcase_19 AC 25 ms
14,148 KB
testcase_20 AC 25 ms
14,088 KB
testcase_21 AC 6 ms
4,696 KB
testcase_22 AC 5 ms
4,380 KB
testcase_23 AC 5 ms
4,424 KB
testcase_24 AC 5 ms
4,512 KB
testcase_25 AC 5 ms
4,624 KB
testcase_26 AC 3 ms
4,384 KB
testcase_27 AC 3 ms
4,376 KB
testcase_28 AC 3 ms
4,380 KB
testcase_29 AC 3 ms
4,376 KB
testcase_30 AC 3 ms
4,376 KB
testcase_31 AC 3 ms
4,376 KB
testcase_32 AC 3 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 3 ms
4,380 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 3 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 3 ms
4,380 KB
testcase_42 AC 2 ms
4,376 KB
testcase_43 AC 24 ms
13,860 KB
testcase_44 AC 25 ms
14,060 KB
testcase_45 AC 25 ms
14,200 KB
testcase_46 AC 25 ms
14,196 KB
testcase_47 AC 24 ms
14,060 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
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto &&x : a)
    {
        cin >> x;
    }
    a.push_back(2);
    vector<int> len, s;
    int k = 0, t = 0;
    for (auto &&x : a)
    {
        if (x == 2)
        {
            len.push_back(k);
            s.push_back(t);
            k = 0;
            t = 0;
        }
        else
        {
            k++;
            t += x;
        }
    }
    int m = len.size(), total = count(a.begin(), a.end(), 1);
    constexpr int inf = 1e9;
    vector dp(m + 1, vector<int>(total + 1, inf));
    dp.at(0).at(0) = 0;
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j <= total; j++)
        {
            dp.at(i + 1).at(j) = min(dp.at(i + 1).at(j), dp.at(i).at(j) + s.at(i));
            if (j + len.at(i) <= total)
            {
                dp.at(i + 1).at(j + len.at(i)) = min(dp.at(i + 1).at(j + len.at(i)), dp.at(i).at(j) + len.at(i) - s.at(i));
            }
        }
    }
    if (dp.at(m).at(total) == inf)
    {
        cout << -1 << endl;
    }
    else
    {
        cout << dp.at(m).at(total) / 2 << endl;
    }
    return 0;
}
0