結果
| 問題 |
No.15 カタログショッピング
|
| コンテスト | |
| ユーザー |
IL_msta
|
| 提出日時 | 2015-08-20 09:04:38 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 14 ms / 5,000 ms |
| コード長 | 2,633 bytes |
| コンパイル時間 | 1,014 ms |
| コンパイル使用メモリ | 94,124 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-18 10:48:55 |
| 合計ジャッジ時間 | 1,510 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 10 |
ソースコード
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <string>
//#include <array>
#include <list>
#include <queue>
#include <vector>
#include <complex>
#include <set>
#include <map>
/////////
#define REP(i, x, n) for(int i = x; i < n; i++)
#define rep(i,n) REP(i,0,n)
#define P(p) cout<<(p)<<endl;
#define PII pair<int,int>
/////////
typedef long long LL;
typedef long double LD;
/////////
using namespace::std;
/////////
int N;
LL tot;
LL A[31];
//vector<pair<LL,int>> hlaf(1<<16);
//priority_queue<pair<LL,int>,vector<pari<LL,int>>,greater<pair<LL.int>> > pq;
vector< pair<LL,int> > Hdata;
priority_queue< pair<LL,int> > pq;
void solveH(LL sum,int n,int bit){
if( n >= N ){
Hdata.push_back( pair<LL,int>(sum,bit) );
//pq.push( pair<LL,int>(sum,bit) );
}else{
solveH( sum + A[n],n+1,bit+(1<<(n-N/2)) );
solveH( sum,n+1, bit);
}
return;
}
void solve(LL sum,int n,int bit){
if( n >= N/2 ){
//check
vector< pair<LL,int> >::iterator it;
it = lower_bound(Hdata.begin(),Hdata.end(),pair<LL,int>(tot-sum,0) );
int tbit;
bool flag = false;
while( it != Hdata.end() && it->first == tot-sum ){
tbit = bit;
flag = false;
rep(i,N/2){
if( tbit % 2 ){
cout << i + 1;
flag = true;
}
tbit = tbit >> 1;
if( tbit && flag ){
cout << " ";
flag = false;
}else if( tbit == 0 ){
break;
}
}
tbit = it->second;
if( flag && tbit ){
cout << " ";
}
flag = false;
for(int i=N/2;i<N;++i){
if( tbit % 2 ){
cout << i + 1;
flag = true;
}
tbit = tbit >> 1;
if( tbit && flag ){
cout << " ";
flag = false;
}else if( tbit == 0 ){
break;
}
}
cout << endl;
++it;
}
return;
}
if( sum + A[n] == tot){
int tbit;
bool flag = false;
tbit = bit + (1<<n);
rep(i,N){
if( tbit % 2 ){
cout << i + 1;
flag = true;
}
tbit = tbit >> 1;
if( tbit && flag ){
cout << " ";
flag = false;
}else if( tbit == 0 ){
cout << endl;
break;
}
}
}else if( sum + A[n] < tot){
solve( sum + A[n],n+1,bit+(1<<n) );
}
solve( sum, n+1, bit);
}
int main(void){
std::cin.tie(0);
std::ios::sync_with_stdio(false);
std::cout << std::fixed;//
//cout << setprecision(16);//
cin>>N>>tot;
rep(i,N){
cin>>A[i];
}
Hdata.reserve( ( 1<<(N/2) ) -1 );
solveH(0,N/2,0);
//priority_queue<pair<LL,int>> test( Hdata.begin() , Hdata.end() );
//pq = priority_queue<pair<LL,int>>( Hdata.begin() , Hdata.end() );
sort( Hdata.begin(), Hdata.end() );
solve(0,0,0);
return 0;
}
IL_msta