結果

問題 No.50 おもちゃ箱
ユーザー assy1028assy1028
提出日時 2015-03-24 13:38:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 250 ms / 5,000 ms
コード長 2,773 bytes
コンパイル時間 1,242 ms
コンパイル使用メモリ 108,672 KB
実行使用メモリ 10,028 KB
最終ジャッジ日時 2023-09-08 03:42:52
合計ジャッジ時間 5,652 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 101 ms
8,340 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 22 ms
4,604 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 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 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 214 ms
9,964 KB
testcase_18 AC 37 ms
4,780 KB
testcase_19 AC 151 ms
9,972 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 30 ms
4,920 KB
testcase_22 AC 215 ms
9,860 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 6 ms
4,376 KB
testcase_28 AC 237 ms
9,896 KB
testcase_29 AC 250 ms
9,944 KB
testcase_30 AC 244 ms
10,028 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 222 ms
9,860 KB
testcase_33 AC 221 ms
9,856 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 166 ms
9,896 KB
testcase_36 AC 219 ms
9,912 KB
testcase_37 AC 176 ms
9,900 KB
testcase_38 AC 247 ms
9,980 KB
testcase_39 AC 12 ms
4,564 KB
testcase_40 AC 35 ms
5,920 KB
testcase_41 AC 247 ms
9,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <map>
#include <numeric>
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <complex>
#include <string.h>
#include <unordered_set>
#include <unordered_map>
#include <tuple>
#include <iomanip>
using namespace std;

#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define uniq(v) (v).erase(unique((v).begin(), (v).end()), (v).end())

typedef long long ll;
typedef pair<int, int> P;
typedef unsigned int uint;
typedef unsigned long long ull;
struct pairhash {
public:
    template<typename T, typename U>
    size_t operator()(const pair<T, U> &x) const {
	size_t seed = hash<T>()(x.first);
	return hash<U>()(x.second) + 0x9e3779b9 + (seed<<6) + (seed>>2);
    }
};

const int inf = 1000000009;
const double eps = 1e-8;

int as[10], bs[10];
int n, m;
set<vector<int> > memo;
string to_binString(int val) {
    if (!val)
	return string("0");

    string ret;
    while(val != 0) {
	if ((val & 1) == 0)
	    ret.insert(ret.begin(), '0');
	else
	    ret.insert(ret.begin(), '1');
	val >>= 1;
    }
    return ret;
}

bool calc(int size, int pnum, int mini, int rem, int bit, vector<int>& vec) {
    if (pnum == size) {
	vector<int> h;
	for (int i = 0; i < size; i++) {
	    int sum = 0;
	    for (int j = 0; j < n; j++)
		if ((vec[i]>>j)&1)
		    sum += as[j];
	    h.push_back(sum);
	}
	sort(rall(h));
	for (int i = 0; i < size; i++) {
	    if (bs[m-i-1] < h[i])
		return false;
	}
	return true;
    } else {
	if (memo.count(vec) == 0) {
	    memo.insert(vec);
	    if (pnum == size-1) {
		vec.push_back(~bit);
		return calc(size, pnum+1, mini, 0, bit, vec);
	    } else {
		for (int i = mini; i * (size-pnum-1) <= rem-i; i++) {
		    //cout << size << " " << pnum << " " << i << endl;
		    int comb = (1 << i) - 1;
		    while (comb < 1 << n) {
			if ((comb&bit) == 0) {
			    //cout << to_binString(comb) << endl; 
			    vector<int> tv = vec;
			    tv.push_back(comb);
			    sort(all(tv));
			    if (calc(size,  pnum+1, i, rem-i, bit|comb, tv))
				return true;
			}
			int x = comb & -comb, y = comb + x;
			comb = ((comb & ~y) / x >> 1) | y;
		    }
		}
	    }
	}
	return false;
    }
}

int solve(int n, int m) {
    for (int i = 1; i <= m; i++) {
	memo.clear();
	vector<int> vec;
	if (calc(i, 0, 1, n, 0, vec))
	    return i;
    }
    return -1;
}

int main() {
    /*
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(15);
    */


    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", &as[i]);
    scanf("%d", &m);
    for (int i = 0; i < m; i++) scanf("%d", &bs[i]);
    sort(bs, bs+m);
    printf("%d\n", solve(n, m));
}
0