結果
| 問題 | No.15 カタログショッピング | 
| コンテスト | |
| ユーザー |  Kude | 
| 提出日時 | 2020-09-03 23:35:01 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 37 ms / 5,000 ms | 
| コード長 | 1,639 bytes | 
| コンパイル時間 | 2,153 ms | 
| コンパイル使用メモリ | 180,692 KB | 
| 実行使用メモリ | 7,552 KB | 
| 最終ジャッジ日時 | 2024-11-24 06:21:38 | 
| 合計ジャッジ時間 | 2,980 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 10 | 
ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
int main() {
    int n;
    ll S;
    cin >> n >> S;
    int n1 = (n + 1) / 2, n2 = n / 2;
    vector<ll> p1(n1), p2(n2);
    rep(i, n1) cin >> p1[i];
    rep(i, n2) cin >> p2[i];
    set<pair<ll, int>> cost_set;
    rep(s, 1 << n1) {
        ll tot = 0;
        rep(k, n1) {
            if (s >> k & 1) {
                tot += p1[k];
            }
        }
        cost_set.emplace(tot, s);
    }
    vector<ll> ans;
    rep(s, 1 << n2) {
        ll tot = 0;
        rep(k, n2) {
            if (s >> k & 1) {
                tot += p2[k];
            }
        }
        auto it = cost_set.lower_bound({S - tot, 0});
        while(it != cost_set.end() && it->first == S - tot) {
            ll t = it->second | ll(s) << n1;
            ans.push_back(t);
            it++;
        }
    }
    sort(all(ans), [](ll x, ll y) {
        while(1) {
            if (x == 0 && x < y) return true;
            if (y == 0) return false;
            int bx = x & 1, by = y & 1;
            if (bx > by) return true;
            else if (bx < by) return false;
            x >>= 1; y >>= 1;
        }
    });
    for(int s: ans) {
        int id = 1;
        while(s) {
            if (s & 1) {
                cout << id;
                if (s) cout << ' ';
            }
            s >>= 1;
            id++;
        }
        cout << endl;
    }
}
            
            
            
        