結果

問題 No.15 カタログショッピング
ユーザー EmKjpEmKjp
提出日時 2015-02-21 16:35:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 54 ms / 5,000 ms
コード長 1,902 bytes
コンパイル時間 1,070 ms
コンパイル使用メモリ 99,544 KB
実行使用メモリ 14,500 KB
最終ジャッジ日時 2023-09-06 02:50:08
合計ジャッジ時間 2,086 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 50 ms
14,468 KB
testcase_06 AC 52 ms
14,356 KB
testcase_07 AC 54 ms
14,492 KB
testcase_08 AC 51 ms
14,500 KB
testcase_09 AC 51 ms
14,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<numeric>
#include<functional>
#include<complex>

using namespace std;
#define BET(a,b,c) ((a)<=(b)&&(b)<(c))
#define FOR(i,n) for(int i=0,i##_end=(int(n));i<i##_end;i++)
#define SZ(x) (int)(x.size())
#define ALL(x) (x).begin(),(x).end()
#define MP make_pair
#define FOR_EACH(it,v) for(__typeof(v.begin()) it=v.begin(),it_end=v.end() ; it != it_end ; it++)
typedef vector<int> VI;
typedef vector<VI> VVI;

void construct(const VI& P, map<int, vector<int>>& sumToBitsTable ){
    int n = SZ(P);
    VI dp(1<<n), bitP(1<<n);
    FOR(i,n) bitP[1<<i] = P[i];
    dp[0] = 0;
    sumToBitsTable[0].push_back(0);
    for(int i=1;i<(1<<n);i++){
        int rightBit = i & (-i);
        dp[i] = dp[i^rightBit] + bitP[rightBit];
        sumToBitsTable[dp[i]].push_back(i);
    }
}

int main()
{
    int N,S;
    cin>>N>>S;
    VI P(N);
    FOR(i,N) cin>>P[i];
    int s1 = N / 2;
    int s2 = N - N / 2;
    VI P1, P2;
    FOR(i,N) {
        if(i < s1) P1.push_back(P[i]);
        else P2.push_back(P[i]);
    }

    map<int, vector<int> > m1, m2;
    construct(P1, m1);
    construct(P2, m2);

    VVI ans;

    for(const auto& p : m1){
        int v1 = p.first;
        int v2 = S - v1;
        auto it = m2.find(v2);
        if(it == m2.end()) continue;
        for(auto b1 : p.second){
            for(auto b2 : it->second) {
                int x = b1 | (b2 << s1);
                VI v;
                FOR(i,N) if(x & (1<<i)) {
                    v.push_back(i);
                }
                ans.push_back(v);
            }
        }
    }
    sort(ALL(ans));
    FOR(i,SZ(ans)) {
        FOR(j,SZ(ans[i])) printf("%s%d",j?" ":"",ans[i][j]+1);
        puts("");
    }
    
    return 0;
}
0