結果
| 問題 | No.15 カタログショッピング |
| コンテスト | |
| ユーザー |
古寺いろは
|
| 提出日時 | 2015-04-15 23:34:44 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 49 ms / 5,000 ms |
| コード長 | 1,198 bytes |
| コンパイル時間 | 1,581 ms |
| コンパイル使用メモリ | 178,716 KB |
| 実行使用メモリ | 12,160 KB |
| 最終ジャッジ日時 | 2024-07-04 14:37:36 |
| 合計ジャッジ時間 | 2,458 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
#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);
}
}
古寺いろは