結果

問題 No.15 カタログショッピング
ユーザー kyo1kyo1
提出日時 2020-06-18 03:13:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 5,000 ms
コード長 1,203 bytes
コンパイル時間 5,508 ms
コンパイル使用メモリ 216,780 KB
実行使用メモリ 12,188 KB
最終ジャッジ日時 2023-09-16 11:55:14
合計ジャッジ時間 3,697 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, S;
  cin >> N >> S;
  vector<int> P(N);
  for (int i = 0; i < N; i++) {
    cin >> P[i];
  }
  int a = N / 2, b = N - a;
  map<int, vector<int>> mp;
  for (int bit = 0; bit < (1 << a); bit++) {
    int x = 0;
    for (int i = 0; i < a; i++) {
      if ((bit >> i) & 1) {
        x += P[i];
      }
    }
    mp[x].emplace_back(bit);
  }
  vector<vector<int>> res;
  for (int bit = 0; bit < (1 << b); bit++) {
    int x = 0;
    for (int i = 0; i < b; i++) {
      if ((bit >> i) & 1) {
        x += P[a + i];
      }
    }
    int s = S - x;
    if (!mp[s].empty()) {
      for (int i = 0; i < (int)mp[s].size(); i++) {
        vector<int> r;
        for (int j = 0; j < a; j++) {
          if ((mp[s][i] >> j) & 1) r.emplace_back(j + 1);
        }
        for (int j = 0; j < b; j++) {
          if ((bit >> j) & 1) r.emplace_back(j + a + 1);
        }
        res.emplace_back(r);
      }
    }
  }
  sort(res.begin(), res.end());
  for (int i = 0; i < (int)res.size(); i++) {
    for (auto &r : res[i]) {
      cout << r << (&r == &res[i].back() ? '\n' : ' ');
    }
  }
  return 0;
}
0