結果

問題 No.15 カタログショッピング
ユーザー NotationNapNotationNap
提出日時 2015-04-23 08:32:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 19 ms / 5,000 ms
コード長 1,066 bytes
コンパイル時間 1,695 ms
コンパイル使用メモリ 164,244 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 08:33:56
合計ジャッジ時間 2,400 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;

typedef long long Int;
#define REP(i,n) for(int (i)=0;(i)<(int)(n);++(i))

Int a[31];
int main() {
	int N;
	Int S;
	cin >> N >> S;
	REP(i, N) cin >> a[i];
	
	int half1 = N / 2;
	int half2 = N - half1;
	vector<pair<Int, int>> v;
	REP(i, 1 << half1) {
		Int s = 0;
		REP(j, half1) {
			if (i >> j & 1) s += a[j];
		}
		v.push_back(make_pair(s, i));
	}
	sort(v.begin(), v.end());

	vector<int> ans;
	REP(iii, 1 << half2) {
		int i = iii << half1;
		Int s = 0;
		REP(j, N) {
			if (i >> j & 1) s += a[j];
		}
	
		int pos = lower_bound(v.begin(), v.end(), make_pair(S - s, -1)) - v.begin();
		while (pos < v.size() && v[pos].first == S - s) {
			ans.push_back(v[pos].second | i);
			pos++;
		}
	}
	
	vector<vector<int>> sa;

	for (auto x : ans) {
		vector<int> y;
		REP(i, N) if (x >> i & 1) {
			y.push_back(i + 1);
		}
		sa.emplace_back(y);
	}
	sort(sa.begin(), sa.end());
	for (auto x : sa) {
		bool first = true;
		for (auto i : x) {
			if (first) first = false; else cout << " ";
			cout << i;
		}
		cout << endl;
	}

}
0