結果

問題 No.15 カタログショッピング
ユーザー IL_mstaIL_msta
提出日時 2015-08-20 09:04:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 14 ms / 5,000 ms
コード長 2,633 bytes
コンパイル時間 886 ms
コンパイル使用メモリ 95,184 KB
実行使用メモリ 5,116 KB
最終ジャッジ日時 2023-09-25 13:45:48
合計ジャッジ時間 1,614 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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;
}
0