結果

問題 No.50 おもちゃ箱
ユーザー PachicobuePachicobue
提出日時 2017-07-14 15:17:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 2,115 bytes
コンパイル時間 1,817 ms
コンパイル使用メモリ 176,080 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-08 04:50:12
合計ジャッジ時間 3,351 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 12 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 13 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 9 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 6 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 4 ms
4,376 KB
testcase_28 AC 13 ms
4,376 KB
testcase_29 AC 13 ms
4,376 KB
testcase_30 AC 9 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 11 ms
4,376 KB
testcase_33 AC 11 ms
4,380 KB
testcase_34 AC 13 ms
4,376 KB
testcase_35 AC 12 ms
4,376 KB
testcase_36 AC 11 ms
4,376 KB
testcase_37 AC 11 ms
4,380 KB
testcase_38 AC 10 ms
4,380 KB
testcase_39 AC 14 ms
4,376 KB
testcase_40 AC 13 ms
4,376 KB
testcase_41 AC 12 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cout << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

int main()
{
    int N;
    cin >> N;
    vector<int> A(N);
    for (ll i = 0; i < N; i++) {
        cin >> A[i];
    }
    sort(A.begin(), A.end());
    int M;
    cin >> M;
    vector<int> B(M);
    for (ll i = 0; i < M; i++) {
        cin >> B[i];
    }
    sort(B.begin(), B.end(), greater<int>());


    const int maximum = 1 << N;
    vector<int> sum(maximum);
    sum[0] = 0;
    for (int i = 1; i < maximum; i++) {
        const int ind = __builtin_ctz(i);
        sum[i] = A[ind] + sum[i - (1 << ind)];
    }


    vector<vector<int>> dp(M + 1, vector<int>(maximum, INF<int>));  // i番目の箱まで使っておもちゃの組み合わせjを収納するときの必要箱数
    dp[0][0] = 0;
    for (int m = 0; m < M; m++) {
        for (int c = 0; c < maximum; c++) {
            if (dp[m][c] != INF<int>) {
                // m+1番目の箱に何も入れない
                dp[m + 1][c] = min(dp[m + 1][c], dp[m][c]);

                // m+1番目の箱に入れる
                for (int addc = 1; addc < maximum; addc++) {  // m+1番目の箱にどれを入れるか
                    if ((c & addc) == 0 and sum[addc] <= B[m]) {
                        dp[m + 1][c | addc] = min(dp[m + 1][c | addc], dp[m][c] + 1);
                    }
                }
            }
        }
    }
    if (dp[M][maximum - 1] != INF<int>) {
        cout << dp[M][maximum - 1] << endl;
    } else {
        cout << -1 << endl;
    }

    return 0;
}
0