結果

問題 No.15 カタログショッピング
ユーザー Ysmr_RyYsmr_Ry
提出日時 2014-12-30 13:23:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 17 ms / 5,000 ms
コード長 1,112 bytes
コンパイル時間 702 ms
コンパイル使用メモリ 59,904 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-03 20:17:36
合計ジャッジ時間 1,452 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<cstdio>
#include<algorithm>
#include<set>
#include<utility>
#include<vector>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define itr(it,a) for(auto it=(a).begin();it!=(a).end();++it)

typedef std::pair<int, int> Pr;

const int MAX_N = 31;

int N, S;
int P[MAX_N];
int S1[1<<16];
Pr S2[1<<16];

int main()
{
	scanf( "%d%d", &N, &S );
	rep( i, N )
		scanf( "%d", P+i );

	rep( i, 1<<(N>>1) ) rep( j, N>>1 ) if( i>>j&1 )
		S1[i] += P[j];
	rep( i, 1<<N-(N>>1) )
	{
		rep( j, N-(N>>1) ) if( i>>j&1 )
			S2[i].first += P[j+(N>>1)];
	
		S2[i].second = i;
	}

	std::sort( S2, S2+(1<<N-(N>>1)) );

	std::set<std::vector<int> > s;
	Pr *p;

	rep( i, 1<<(N>>1) ) if( (p = std::lower_bound( S2, S2+(1<<N-(N>>1)), Pr( S-S1[i], 0 ) ))->first == S-S1[i] )
	{
		do
		{
			std::vector<int> v;

			rep( k, N>>1 ) if( i>>k&1 )
				v.push_back( k+1 );
			rep( k, N-(N>>1) ) if( p->second>>k&1 )
				v.push_back( k+1+(N>>1) );
			
			s.insert( v );
		} while( (++p)->first == S-S1[i] );
	}

	itr( it, s ) rep( i, it->size() )
		printf( "%d%c", (*it)[i], i==it->size()-1?'\n':' ' );

	return 0;
}
0