結果
問題 | No.15 カタログショッピング |
ユーザー |
![]() |
提出日時 | 2020-11-08 22:30:41 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 103 ms / 5,000 ms |
コード長 | 1,359 bytes |
コンパイル時間 | 1,996 ms |
コンパイル使用メモリ | 190,984 KB |
実行使用メモリ | 18,560 KB |
最終ジャッジ日時 | 2024-07-22 15:45:34 |
合計ジャッジ時間 | 3,126 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 10 |
ソースコード
#include <bits/stdc++.h> using namespace std; int main(){ int N, S; cin >> N >> S; vector<int> P(N); for (int i = 0; i < N; i++){ cin >> P[i]; } int A = N / 2; int B = N - A; map<int, vector<vector<int>>> mpA; for (int i = 0; i < (1 << A); i++){ int sum = 0; vector<int> s; for (int j = 0; j < A; j++){ if (i >> j & 1){ sum += P[j]; s.push_back(j); } } mpA[sum].push_back(s); } map<int, vector<vector<int>>> mpB; for (int i = 0; i < (1 << B); i++){ int sum = 0; vector<int> s; for (int j = 0; j < B; j++){ if (i >> j & 1){ sum += P[A + j]; s.push_back(A + j); } } mpB[sum].push_back(s); } vector<vector<int>> ans; for (auto p : mpB){ if (mpA.count(S - p.first)){ for (auto a : mpA[S - p.first]){ for (auto b : p.second){ vector<int> tmp; for (int x : a){ tmp.push_back(x); } for (int x : b){ tmp.push_back(x); } ans.push_back(tmp); } } } } sort(ans.begin(), ans.end()); int cnt = ans.size(); for (int i = 0; i < cnt; i++){ int cnt2 = ans[i].size(); for (int j = 0; j < cnt2; j++){ cout << ans[i][j] + 1; if (j < cnt2 - 1){ cout << ' '; } } cout << endl; } }