結果

問題 No.50 おもちゃ箱
ユーザー codershifth
提出日時 2015-07-27 21:14:44
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 123 ms / 5,000 ms
コード長 1,762 bytes
コンパイル時間 1,470 ms
コンパイル使用メモリ 163,940 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-25 20:57:27
合計ジャッジ時間 3,299 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

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