結果

問題 No.15 カタログショッピング
ユーザー kimiyukikimiyuki
提出日時 2017-01-08 14:44:01
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,396 ms / 5,000 ms
コード長 1,179 bytes
コンパイル時間 1,266 ms
コンパイル使用メモリ 93,980 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-22 17:37:25
合計ジャッジ時間 5,318 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#define repeat(i,n) for (int i = 0; (i) < int(n); ++(i))
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
using ll = long long;
using namespace std;
int main() {
    int n; ll s; cin >> n >> s;
    vector<ll> p(n); repeat (i,n) cin >> p[i];
    vector<int> tr(n); whole(iota, tr, 0);
    whole(sort, tr, [&](int i, int j) { return p[i] > p[j]; });
    vector<ll> acc(n+1); repeat (i,n) acc[i+1] = acc[i] + p[tr[i]];
    vector<vector<int> > result;
    vector<int> path;
    function<void (int, ll)> go = [&](int i, ll q) {
        if (q == s) result.push_back(path);
        if (q >= s) return;
        if (q + acc[n] - acc[i] < s) return;
        if (i == n) return;
        go(i+1, q);
        path.push_back(tr[i]);
        go(i+1, q + p[tr[i]]);
        path.pop_back();
    };
    go(0, 0);
    for (auto & path : result) whole(sort, path);
    whole(sort, result);
    for (auto & path : result) {
        repeat (i,path.size()) cout << (i ? " " : "") << path[i]+1;
        cout << endl;
    }
    return 0;
}
0