結果

問題 No.15 カタログショッピング
ユーザー imulanimulan
提出日時 2016-12-10 03:27:51
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 46 ms / 5,000 ms
コード長 1,516 bytes
コンパイル時間 2,207 ms
コンパイル使用メモリ 187,224 KB
実行使用メモリ 8,060 KB
最終ジャッジ日時 2023-08-19 08:12:54
合計ジャッジ時間 3,094 ms
ジャッジサーバーID
(参考情報)
judge13 / judge9
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 42 ms
8,060 KB
testcase_06 AC 44 ms
7,988 KB
testcase_07 AC 45 ms
8,036 KB
testcase_08 AC 44 ms
7,920 KB
testcase_09 AC 46 ms
7,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

typedef vector<int> vi;
typedef pair<int,vector<int>> P;

int main()
{
    int n,s;
    scanf(" %d %d", &n, &s);
    vi p(n);
    rep(i,n) scanf(" %d", &p[i]);

    int A=n/2;
    int B=n-A;

    vector<P> sumB;

    rep(i,1<<B)
    {
        int sum=0;
        vi t;
        rep(j,B)
        {
            if(i>>j&1)
            {
                t.pb(A+j);
                sum+=p[A+j];
            }
        }

        sumB.pb(P(sum,t));
    }
    sort(all(sumB));

    vector<vi> ans;
    rep(i,1<<A)
    {
        int sum=0;
        vi t;
        rep(j,A)
        {
            if(i>>j&1)
            {
                t.pb(j);
                sum+=p[j];
            }
        }

        if(sum>s) continue;

        int idx = lower_bound(all(sumB),P(s-sum,vi())) - sumB.begin();

        while(idx<sumB.size() && sum+sumB[idx].fi==s)
        {
            vi tmp=sumB[idx].se;
            vi tt(t);
            rep(j,tmp.size()) tt.pb(tmp[j]);

            ans.pb(tt);
            ++idx;
        }
    }

    sort(all(ans));

    rep(i,ans.size())
    {
        rep(j,ans[i].size())
        {
            if(j) printf(" ");
            printf("%d", ans[i][j]+1);
        }
        printf("\n");
    }
    return 0;
}
0