結果

問題 No.15 カタログショッピング
ユーザー PachicobuePachicobue
提出日時 2017-06-04 05:48:53
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 13 ms / 5,000 ms
コード長 2,709 bytes
コンパイル時間 2,164 ms
コンパイル使用メモリ 183,612 KB
実行使用メモリ 4,688 KB
最終ジャッジ日時 2023-10-22 03:26:18
合計ジャッジ時間 2,624 ms
ジャッジサーバーID
(参考情報)
judge10 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 11 ms
4,688 KB
testcase_06 AC 12 ms
4,688 KB
testcase_07 AC 13 ms
4,688 KB
testcase_08 AC 13 ms
4,688 KB
testcase_09 AC 12 ms
4,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep1(i, n) for (ll i = 1; i <= (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)

#define pb push_back
#define mp make_pair
#define fst first
#define snd second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define pii pair<int, int>
#define vi vector<int>

using namespace std;
template <class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& p)
{
    return o << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& vc)
{
    o << "sz = " << vc.size() << endl
      << "[";
    for (const T& v : vc)
        o << v << ",";
    o << "]";
    return o;
}
using ll = long long;
constexpr ll MOD = 1000000007;

void decode(ll c, ll d)
{
    stack<ll> st;
    for (ll i = 0; i < d; i++) {
        if ((c >> i) % 2) {
            st.push(d - i);
        }
    }
    bool first = true;
    while (not st.empty()) {
        if (not first) {
            cout << " ";
        }
        first = false;
        cout << st.top();
        st.pop();
    }
    cout << endl;
}

int main()
{
    ll n;
    ll s;
    cin >> n >> s;
    vector<ll> p(n);
    rep(i, n)
    {
        cin >> p[n - 1 - i];
    }
    const ll former = n / 2;
    const ll latter = n - former;
    const ll formernum = (1LL << former);
    const ll latternum = (1LL << latter);

    vector<ll> formersum(formernum);
    formersum[0] = 0;
    for (ll c = 1; c < formernum; c++) {
        for (ll b = 0; b < former; b++) {
            if ((c >> b) % 2) {
                formersum[c] = formersum[c - (1LL << b)] + p[b];
                break;
            }
        }
    }

    vector<pair<ll, ll>> lattersum(latternum);
    lattersum[0] = mp(0LL, 0LL);
    for (ll c = 1; c < latternum; c++) {
        for (ll b = 0; b < latter; b++) {
            if ((c >> b) % 2) {
                lattersum[c] = mp(lattersum[c - (1LL << b)].first + p[b + former], c << former);
                break;
            }
        }
    }

    sort(lattersum.begin(), lattersum.end());

    vector<ll> choice;
    for (ll fc = 0; fc < formernum; fc++) {
        const ll rest = s - formersum[fc];
        auto ite = lower_bound(lattersum.begin(), lattersum.end(), mp(rest, -1LL));
        while (ite->first == rest) {
            choice.pb(ite->second + fc);
            ite++;
        }
    }

    sort(choice.begin(), choice.end(), greater<ll>());
    for (const ll c : choice) {
        decode(c, n);
    }

    return 0;
}
0