結果

問題 No.50 おもちゃ箱
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-09-15 20:30:11
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 248 ms / 5,000 ms
コード長 1,250 bytes
コンパイル時間 2,082 ms
コンパイル使用メモリ 67,084 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-08 04:34:00
合計ジャッジ時間 3,484 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);i++)
const int INF = 1e9;
int n, m;

int main(void){
	cin >> n;
	vector<int> A(n);
	rep(i, n) cin >> A[i];
	sort(A.begin(), A.end());
	cin >> m;
	vector<int> B(m);
	rep(i, m) cin >> B[i];
	//箱は大きいものから使ったほうがいい
	sort(B.begin(), B.end());
	reverse(B.begin(), B.end());

	int ans = INF;
	//商品を入れていく順番をすべて試す (n!)
	do{
		vector<int> hako(m);
		rep(i, m) hako[i] = B[i];
		int tm = 0;//どこ箱まで使ったかをメモするもの
		bool flag = false;

		for (int i = 0; i < n ; ++i){//順番におもちゃを入れていく
			for (int j = 0; j < m; ++j){//箱を大きいものから使っていく
				if(hako[j] - A[i] >= 0){
					hako[j] -= A[i];//容量を減らす
					tm = max(tm, j);
					break;
				}
				if(j == m - 1){
					flag = true;
					break;
				}
			}
			if(flag) break;
		}
		if(flag) continue;//すべての荷物が入っている時のみ、ansを更新
		ans = min(ans, tm);
	}while(next_permutation(A.begin(), A.end()));

	if(ans != INF) cout << ans  + 1 << endl;
	else cout << -1 << endl;
	return 0;
}
0