結果

問題 No.15 カタログショッピング
ユーザー codershifthcodershifth
提出日時 2015-07-20 14:54:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 22 ms / 5,000 ms
コード長 2,661 bytes
コンパイル時間 2,766 ms
コンパイル使用メモリ 164,532 KB
実行使用メモリ 4,472 KB
最終ジャッジ日時 2023-09-22 20:35:25
合計ジャッジ時間 3,576 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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,int>> S1;
            int m = N;
            if (m > 16)
                m = 16;
            // O(2^17*m*log(N))
            for (int b = 0; b < (1<<m); ++b)
            {
                ll sum = 0;
                FOR(i,1,m+1) if (b & (1<<(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 < (1<<(N-m)); ++b)
            {
                FOR(i,1,N-m+1) if (b & (1<<(N-m-i)))
                    S2[b] += P[i+m-1];
            }
            // 二分探索
            // O(2^(N-m)*16)
            vector<int> 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, -1));
                auto it2 = upper_bound(RANGE(S1), make_pair(S-s2, (1<<30)));
                if (it1 == S1.end() || it1->first != S-s2)
                    continue;
                while (it1 != it2)
                {
                    int b1;
                    ll  s1;
                    tie(s1,b1) = *it1;
                    combs.push_back(b1<<(N-m) | b2);
                    ++it1;
                }
            }
            sort(RANGE(combs), greater<int>());
            for (auto b : combs)
            {
                string ans;
                FOR(i,1,N+1) if (b & (1<<(N-i)))
                    ans+=to_string(i)+" ";
                cout<<ans.substr(0,ans.length()-1)<<endl;
            }
    }
};

#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