結果

問題 No.15 カタログショッピング
ユーザー momoyuumomoyuu
提出日時 2023-03-16 16:11:27
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 5,000 ms
コード長 1,623 bytes
コンパイル時間 3,708 ms
コンパイル使用メモリ 270,848 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-18 13:04:39
合計ジャッジ時間 4,548 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 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 18 ms
4,356 KB
testcase_06 AC 21 ms
4,356 KB
testcase_07 AC 22 ms
4,356 KB
testcase_08 AC 23 ms
4,356 KB
testcase_09 AC 22 ms
4,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int main(){
    int n;
    cin >> n;
    ll s;
    cin>>s;
    int m = n / 2;
    int mm = n - m;
    vector<ll> a(m),b(mm);
    for(int i = 0;i<m;i++) cin>>a[i];
    for(int i = 0;i<mm;i++) cin>>b[i];
    vector<pair<ll,ll> > d;
    for(int i = 0;i<1<<mm;i++){
        ll sum = 0;
        for(int j = 0;j<mm;j++){
            if(i>>j&1) sum += b[j];
        }
        d.push_back(make_pair(sum,i));
        //cout<<sum<<" "<<i<<endl;
    }
    vector<vector<ll> > ans;
    vector<pair<ll,ll> > ss;
    sort(d.begin(),d.end());
    for(int i = 0;i<1<<m;i++){
        ll sum = 0;
        for(int j = 0;j<m;j++){
            if(i>>j&1) sum += a[j];
        }
        ll want = s - sum;
        int ni = lower_bound(d.begin(),d.end(),make_pair(want,-1LL)) - d.begin();
        int nj = upper_bound(d.begin(),d.end(),make_pair(want,(ll)1e9)) - d.begin();
        //cout<<sum<<" "<<ni<<" "<<nj<<endl;
        for(int j = ni;j<nj;j++) ss.push_back(make_pair(i,d[j].second));
    }

    for(int i = 0;i<ss.size();i++){
        vector<ll> now;
        int ni = ss[i].first;
        int nj = ss[i].second;
        //cout<<ni<<" "<<nj<<endl;
        for(int j = 0;j<m;j++){
            if(ni>>j&1) now.push_back(j+1);
        }
        for(int j = 0;j<mm;j++){
            if(nj>>j&1) now.push_back(m+j+1);
        }
        ans.push_back(now);
    }
    sort(ans.begin(),ans.end());
    for(int i = 0;i<ans.size();i++){
        for(int j = 0;j<ans[i].size();j++){
            if(j!=0) cout<<" ";
            cout<<ans[i][j];
        }
        cout<<endl;
    }

}

0