結果

問題 No.1861 Required Number
ユーザー 👑 hitonanodehitonanode
提出日時 2023-05-10 06:17:59
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 92 ms / 2,500 ms
コード長 1,147 bytes
コンパイル時間 1,091 ms
コンパイル使用メモリ 98,124 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-04 21:39:53
合計ジャッジ時間 7,706 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 72 ms
5,376 KB
testcase_04 AC 71 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 42 ms
5,376 KB
testcase_25 AC 86 ms
5,376 KB
testcase_26 AC 50 ms
5,376 KB
testcase_27 AC 77 ms
5,376 KB
testcase_28 AC 66 ms
5,376 KB
testcase_29 AC 83 ms
5,376 KB
testcase_30 AC 88 ms
5,376 KB
testcase_31 AC 86 ms
5,376 KB
testcase_32 AC 82 ms
5,376 KB
testcase_33 AC 83 ms
5,376 KB
testcase_34 AC 86 ms
5,376 KB
testcase_35 AC 91 ms
5,376 KB
testcase_36 AC 85 ms
5,376 KB
testcase_37 AC 92 ms
5,376 KB
testcase_38 AC 84 ms
5,376 KB
testcase_39 AC 81 ms
5,376 KB
testcase_40 AC 91 ms
5,376 KB
testcase_41 AC 89 ms
5,376 KB
testcase_42 AC 91 ms
5,376 KB
testcase_43 AC 88 ms
5,376 KB
testcase_44 AC 2 ms
5,376 KB
04_evil_1.txt AC 864 ms
6,836 KB
04_evil_2.txt AC 859 ms
6,964 KB
04_evil_3.txt AC 848 ms
7,224 KB
04_evil_4.txt AC 837 ms
7,084 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bitset>
#include <iostream>
#include <vector>
using namespace std;

std::vector<std::vector<int>> knapsack_first_fit(const std::vector<int> &A, int K) {
    std::bitset<100001> bs;

    bs.set(0);
    std::vector<std::vector<int>> t2add(1, {0});

    for (int a : A) {
        auto diff = (bs << a) & ~bs;
        t2add.push_back({});
        for (int j = diff._Find_first(); j <= K; j = diff._Find_next(j)) {
            bs.set(j);
            t2add.back().push_back(j);
        }
    }
    return t2add;
}

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

    int N, K;
    cin >> N >> K;
    vector<int> A(N);
    for (auto &x : A) cin >> x;

    auto t2add = knapsack_first_fit(A, K);

    bitset<100001> left, right;
    for (const auto &is : t2add) for (int i : is) left.set(i);

    if (!left.test(K)) {
        puts("-1");
    } else {
        int ret = 0;
        right.set(K);

        for (int i = N - 1; i >= 0; --i) {
            for (int j : t2add.at(i + 1)) left.reset(j);
            if ((right & left).none()) ++ret;
            right |= right >> A.at(i);
        }
        cout << ret << endl;
    }

}
0