結果
| 問題 |
No.15 カタログショッピング
|
| コンテスト | |
| ユーザー |
codershifth
|
| 提出日時 | 2015-07-20 14:48:48 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,711 bytes |
| コンパイル時間 | 1,648 ms |
| コンパイル使用メモリ | 175,224 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-08 11:13:26 |
| 合計ジャッジ時間 | 2,195 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 WA * 6 |
ソースコード
#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
codershifth