結果

問題 No.15 カタログショッピング
ユーザー 古寺いろは古寺いろは
提出日時 2015-04-15 23:34:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 59 ms / 5,000 ms
コード長 1,198 bytes
コンパイル時間 1,577 ms
コンパイル使用メモリ 164,640 KB
実行使用メモリ 12,244 KB
最終ジャッジ日時 2023-09-17 20:28:14
合計ジャッジ時間 2,681 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

void output(vector<int> v){
	for (int i = 0; i < v.size(); i++)
	{
		if (i != v.size() - 1) cout << v[i] << " ";
		else cout << v[i] << endl;
	}
}

int main() {
	int N;
	long long S;
	cin >> N >> S;
	vector<int> P(N);
	for (int i = 0; i < N; i++)
	{
		cin >> P[i];
	}

	if (N == 1){
		if (P[0] == S){
			cout << 1 << endl;
		}
		return 0;
	}

	int a = N / 2;
	int b = N - a;

	map<long long, vector<int>> m;
	for (int i = 0; i < (1<<a); i++)
	{
		long long sum = 0;
		for (int j = 0; j < a; j++)
		{
			if ((i >> j) % 2 == 0) continue;
			sum += P[j];
		}
		m[sum].push_back(i);
	}
	vector<vector<int>> answers;
	for (int i = 0; i < (1 << b); i++)
	{
		long long sum = 0;
		for (int j = 0; j < b; j++)
		{
			if ((i >> j) % 2 == 0) continue;
			sum += P[a + j];
		}
		long long need = S - sum;
		for (auto ar : m[need]){
			vector<int> ans;
			for (int j = 0; j < a; j++)
			{
				if ((ar >> j) % 2 == 1) ans.push_back(j + 1);
			}
			for (int j = 0; j < b; j++)
			{
				if ((i >> j) % 2 == 1) ans.push_back(a + j + 1);
			}
			answers.push_back(ans);
		}
	}
	sort(answers.begin(), answers.end());

	for (auto ans: answers)
	{
		output(ans);
	}
}



0