結果

問題 No.15 カタログショッピング
ユーザー codershifthcodershifth
提出日時 2015-07-20 14:48:48
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,711 bytes
コンパイル時間 1,565 ms
コンパイル使用メモリ 162,712 KB
実行使用メモリ 4,620 KB
最終ジャッジ日時 2023-09-22 20:34:28
合計ジャッジ時間 2,190 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class CatalogueShopping {
public:
    void solve(void) {
            int N;
            ll  S;
            cin>>N>>S;

            vector<ll> P(N);
            REP(i, N)
                cin>>P[i];

            // ∑P[i] = S なる I をみつければよい
            // i in I
            //
            // S が大きすぎるので DP は無理
            // N が小さいので 2^N 全探索試すか?
            // 2^31 = 2147483648 > 10^7*200 なので無理

            // 前半・後半列挙でやる
            vector<pair<ll,long>> S1;
            int m = N;
            if (m > 16)
                m = 16;
            // O(2^17*m*log(N))
            for (long b = 0; b < (1L<<m); ++b)
            {
                ll sum = 0;
                FOR(i,1,m+1) if (b & (1L<<(m-i)))
                    sum += P[i-1];
                S1.emplace_back(sum, b);
            }
            sort(RANGE(S1));
            vector<ll> S2((1<<(N-m)), 0);
            // O(2^17*N)
            for (int b = 0; b < (1L<<(N-m)); ++b)
            {
                FOR(i,1,N-m+1) if (b & (1L<<(N-m-i)))
                    S2[b] += P[i+m-1];
            }
            // 二分探索
            // O(2^(N-m)*16)
            vector<long> combs;
            REP(b2, 1<<(N-m))
            {
                ll s2 = S2[b2];
                if (s2 == S)
                {
                    combs.push_back(b2);
                    continue;
                }
                auto it1 = lower_bound(RANGE(S1), make_pair(S-s2, 0L));
                auto it2 = upper_bound(RANGE(S1), make_pair(S-s2, (1L<<30)));
                if (it1 == S1.end() || it1->first != S-s2)
                    continue;
                while (it1 != it2)
                {
                    long b1;
                    ll  s1;
                    tie(s1,b1) = *it1;
                    combs.push_back(b1<<(N-m) | b2);
                    ++it1;
                }
            }
            sort(RANGE(combs), greater<int>());
            for (auto b : combs)
            {
                FOR(i,1,N+1) if (b & (1<<(N-i)))
                {
                    if (i == N)
                        cout<<i<<endl;
                    else
                        cout<<i<<" ";
                }
            }
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new CatalogueShopping();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0