結果

問題 No.15 カタログショッピング
ユーザー togari_takamototogari_takamoto
提出日時 2016-02-25 18:21:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 72 ms / 5,000 ms
コード長 1,652 bytes
コンパイル時間 1,546 ms
コンパイル使用メモリ 178,972 KB
実行使用メモリ 15,636 KB
最終ジャッジ日時 2023-10-23 20:02:33
合計ジャッジ時間 2,631 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 66 ms
15,636 KB
testcase_06 AC 71 ms
15,636 KB
testcase_07 AC 72 ms
15,636 KB
testcase_08 AC 72 ms
15,636 KB
testcase_09 AC 68 ms
15,636 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi  = vector<int>; using vb  = vector<bool>; using vd  = vector<double>; using vl  = vector<ll>;
using vvi = vector<vi>;  using vvb = vector<vb>;   using vvd = vector<vd>;     using vvl = vector<vl>;

#define REP(i,n) for(ll i=0; i<(n); ++i)
#define FOR(i,b,n) for(ll i=(b); i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()

void dfs(const vl & p, ll index, vi v, vector<pair<vi, ll>> & p_1) {
	if (index == p.size()/2) {
		ll sum = 0; for(auto i : v) sum += p[i];
		p_1.push_back({v, sum});
		return;
	}

	v.push_back(index);
	dfs(p, index+1, v, p_1);
	v.pop_back();
	dfs(p, index+1, v, p_1);
}

void dfs2(const vl & p, ll index, vi v, map<ll, vvi> & p_2) {
	if (index == p.size()) {
		ll sum = 0; for(auto i : v) sum += p[i];
		p_2[sum].push_back(v);
		return;
	}
	
	v.push_back(index);
	dfs2(p, index+1, v, p_2);
	v.pop_back();
	dfs2(p, index+1, v, p_2);
}

int main() {
	// cin.tie(0);
	// ios_base::sync_with_stdio(false);
	// cout << fixed << setprecision(5);

	ll n, s;
	cin >> n >> s;

	vl p(n);
	for (auto&&i : p) cin >> i;

	vector<pair<vi, ll>> p_1;
	dfs(p, 0, {}, p_1);

	map<ll, vvi> p_2;
	dfs2(p, p.size()/2, {}, p_2);

	for (auto & _ : p_1) {
		auto it = p_2.find(s - _.second);
		if (it == p_2.end()) continue;

		auto & item1 = _.first;
		for (auto & item2 : it->second) {
			if (!item1.empty()) cout << item1[0]+1;
			FOR(i, 1, item1.size()) cout << " " << item1[i]+1;

			if (!item1.empty() && !item2.empty()) cout << " ";

			if (!item2.empty()) cout << item2[0]+1;
			FOR(i, 1, item2.size()) cout << " " << item2[i]+1;
			cout << endl;
		}
	}

	return 0;
}
0