結果

問題 No.50 おもちゃ箱
ユーザー codershifthcodershifth
提出日時 2015-07-27 21:14:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 131 ms / 5,000 ms
コード長 1,762 bytes
コンパイル時間 1,199 ms
コンパイル使用メモリ 150,492 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 03:45:20
合計ジャッジ時間 3,359 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class ToyBox {
public:
    void solve(void) {
            int N, M;
            cin>>N;
            vector<int> A(N);
            REP(i,N)
                cin>>A[i];
            cin>>M;
            vector<int> B(M);
            REP(i,M)
                cin>>B[i];

            // N が小さいので全探索で間に合う
            sort(RANGE(A));

            int ans = (1<<30);
            // 同じ数の箱を用意するなら容量のおきいものの方がよいので
            // 容量が大きいもの順に並べておく
            sort(RANGE(B));
            reverse(RANGE(B));

            do {
                auto box = B;
                int i = 0;
                int j = 0;
                bool ok = true;
                while (true)
                {
                    if (A[i] > box[j])
                        ++j;
                    if (j == M || A[i] > box[j])
                    {
                        ok = false;
                        break;
                    }
                    box[j] -= A[i];
                    ++i;
                    if (i == N)
                        break;
                }
                if (ok)
                    ans = min(ans, j+1);
            } while (next_permutation(RANGE(A)));
            if (ans > M)
                ans = -1;
            cout<<ans<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new ToyBox();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0