結果

問題 No.15 カタログショッピング
ユーザー mayoko_mayoko_
提出日時 2015-07-19 08:33:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 22 ms / 5,000 ms
コード長 2,347 bytes
コンパイル時間 2,961 ms
コンパイル使用メモリ 99,956 KB
実行使用メモリ 4,692 KB
最終ジャッジ日時 2023-09-22 19:14:26
合計ジャッジ時間 3,732 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//#include<cctype>
#include<climits>
#include<iostream>
#include<string>
#include<vector>
#include<map>
//#include<list>
#include<queue>
#include<deque>
#include<algorithm>
//#include<numeric>
#include<utility>
#include<complex>
//#include<memory>
#include<functional>
#include<cassert>
#include<set>

const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef complex<double> C;

const int MAXN = 32;
int P[MAXN];
struct pll {
    ll first;
    ll second;
};

bool operator<(const pll& lhs, const pll& rhs) {
    return lhs.first < rhs.first;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, S;
    cin >> N >> S;
    for (int i = 0; i < N; i++) {
        cin >> P[i];
    }
    int n = N/2;
    vector<pll> first, second;
    for (ll s = 0; s < (1<<n); s++) {
        pll item;
        item.first = 0;
        item.second = s;
        for (int i = 0; i < n; i++) {
            if ((s>>i)&1) item.first += P[i];
        }
        first.push_back(item);
    }
    for (ll s = 0; s < (1<<(N-n)); s++) {
        pll item;
        item.first = 0;
        item.second = (s<<n);
        for (int i = 0; i < (N-n); i++) {
            if ((s>>i)&1) item.first += P[i+n];
        }
        second.push_back(item);
    }
    sort(first.begin(), first.end());
    sort(second.begin(), second.end());
    vector<ll> ans;
    for (pll p : first) {
        pll q;
        q.first = S-p.first;
        q.second = p.second;
        auto it1 = lower_bound(second.begin(), second.end(), q);
        auto it2 = upper_bound(second.begin(), second.end(), q);
        for (auto it = it1; it < it2; it++) {
            ans.push_back(p.second|(it->second));
        }
    }
    vector<vi> ANS;
    for (ll el : ans) {
        vi tmp;
        for (int i = 0; i < 32; i++) {
            if ((el>>i)&1) tmp.push_back(i+1);
        }
        sort(tmp.begin(), tmp.end());
        ANS.push_back(tmp);
    }
    sort(ANS.begin(), ANS.end());
    for (vi v : ANS) {
        int n = v.size();
        for (int i = 0; i < n; i++) {
            cout << v[i];
            if (i != n-1) cout << " ";
        }
        cout << endl;
    }
    return 0;
}
0