結果

問題 No.15 カタログショッピング
ユーザー SSRSSSRS
提出日時 2020-11-08 22:30:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 5,000 ms
コード長 1,359 bytes
コンパイル時間 2,153 ms
コンパイル使用メモリ 188,068 KB
実行使用メモリ 18,468 KB
最終ジャッジ日時 2023-09-29 21:44:42
合計ジャッジ時間 3,242 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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;
  }
}
0