結果

問題 No.15 カタログショッピング
ユーザー pekempeypekempey
提出日時 2015-09-04 15:37:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 21 ms / 5,000 ms
コード長 1,544 bytes
コンパイル時間 2,275 ms
コンパイル使用メモリ 163,000 KB
実行使用メモリ 4,660 KB
最終ジャッジ日時 2023-09-26 04:20:48
合計ジャッジ時間 2,498 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 18 ms
4,604 KB
testcase_06 AC 19 ms
4,608 KB
testcase_07 AC 20 ms
4,600 KB
testcase_08 AC 21 ms
4,660 KB
testcase_09 AC 20 ms
4,620 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) for (int i = (a) - 1; i >= 0; i--)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
using namespace std;
typedef long long ll;

vector<pair<ll, ll>> bruteforce(vector<ll> P) {
	int n = P.size();
	vector<pair<ll, ll>> res;
	rep (i, 1 << n) {
		ll total = 0;
		rep (j, n) {
			if (i & 1 << j) total += P[j];	
		}
		res.emplace_back(total, i);
	}
	sort(res.begin(), res.end());
	return res;
}

template<class T>
ostream &operator <<(ostream &os, const vector<T> &v) {
	rep (i, v.size()) {
		if (i) os << " ";
		os << v[i];
	}
	return os;
}

int main() {
	ll N, S;
	cin >> N >> S;
	vector<ll> P(N);
	rep (i, N) cin >> P[i];
	int l = N / 2;
	int r = (N + 1) / 2;
	vector<ll> vl, vr;
	rep (i, N) {
		if (i < l) {
			vl.push_back(P[N - 1 - i]);
		} else {
			vr.push_back(P[N - 1 - i]);
		}
	}
	auto ul = bruteforce(vl);
	auto ur = bruteforce(vr);
	vector<ll> ans;
	rep (i, ul.size()) {
		int lb = lower_bound(ur.begin(), ur.end(), make_pair(S - ul[i].first, 0ll)) - ur.begin();
		int ub = upper_bound(ur.begin(), ur.end(), make_pair(S - ul[i].first, (ll)1e18)) - ur.begin();
		rep2 (j, lb, ub) {
			ans.push_back(ul[i].second + (ur[j].second << l));
		}
	}
	sort(ans.begin(), ans.end());
	reverse(ans.begin(), ans.end());
	rep (i, ans.size()) {
		vector<int> v;
		rep (j, N) {
			if (ans[i] & 1 << j) v.push_back(N - j);
		}
		reverse(v.begin(), v.end());
		cout << v << endl;
	}

	return 0;
}
0