結果

問題 No.15 カタログショッピング
ユーザー face4face4
提出日時 2019-02-03 15:21:35
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 98 ms / 5,000 ms
コード長 1,528 bytes
コンパイル時間 1,522 ms
コンパイル使用メモリ 102,944 KB
実行使用メモリ 16,384 KB
最終ジャッジ日時 2023-08-22 14:37:37
合計ジャッジ時間 2,832 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;

int main(){
    int n, s;
    cin >> n >> s;

    vector<int> v(n);
    for(int i = 0; i < n; i++)  cin >> v[i];

    vector<pair<int,set<int>>> vp;
    int n1 = n/2;
    for(int i = 0; i < 1<<n1; i++){
        set<int> tmp;
        int tmpval = 0;
        for(int j = 0; j < n1; j++){
            if((i>>j)&1){
                tmp.insert(j+1);
                tmpval += v[j];
            }
        }
        vp.push_back({tmpval, tmp});
    }
    sort(vp.begin(), vp.end());
    
    vector<set<int>> ans;
    for(int i = 0; i < 1<<(n-n1); i++){
        set<int> tmp;
        int tmpval = 0;
        for(int j = 0; j < n-n1; j++){
            if((i>>j)&1){
                tmp.insert(n1+j+1);
                tmpval += v[n1+j];
            }
        }
        if(tmpval > s)  continue;
        
        set<int> emp;
        int l = lower_bound(vp.begin(), vp.end(), make_pair(s-tmpval, emp)) - vp.begin();
        int r = lower_bound(vp.begin(), vp.end(), make_pair(s-tmpval+1, emp)) - vp.begin();
        r--;

        while(l <= r){
            set<int> copy = vp[l].second;
            copy.insert(tmp.begin(), tmp.end());
            ans.push_back(copy);
            l++;
        }
    }

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

    for(set<int> t : ans){
        for(auto it = t.begin(); it != t.end(); it++){
            if(it != t.begin()) cout << " ";
            cout << *it;
        }
        cout << endl;
    }

    return 0;
}
0