結果

問題 No.15 カタログショッピング
ユーザー cielciel
提出日時 2014-11-17 17:15:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 56 ms / 5,000 ms
コード長 1,088 bytes
コンパイル時間 1,548 ms
コンパイル使用メモリ 80,984 KB
実行使用メモリ 7,112 KB
最終ジャッジ日時 2023-08-30 20:25:29
合計ジャッジ時間 2,518 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,388 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 44 ms
7,084 KB
testcase_06 AC 51 ms
7,112 KB
testcase_07 AC 56 ms
7,084 KB
testcase_08 AC 55 ms
7,092 KB
testcase_09 AC 54 ms
7,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <map>
#include <algorithm>
#include <cstdio>
using namespace std;
int main(){
	int n;
	long long s;
	scanf("%d%lld",&n,&s);
	vector<long long>v(n);
	for(int i=0;i<n;i++)scanf("%lld",&v[i]);
	if(n==1){
		if(v[0]==s)puts("1");
		return 0;
	}
	int mid=n/2;
	multimap<long long,vector<int> >mm;
	for(int i=0;i<1<<mid;i++){
		long long val=0;
		vector<int>lst;
		for(int j=0;j<mid;j++)if(i&(1<<j)){
			val+=v[j];
			lst.push_back(j+1);
		}
		auto it=mm.upper_bound(val);
		mm.insert(make_pair(val,lst));
	}
	vector<vector<int> >result;
	for(int i=0;i<1<<(n-mid);i++){
		long long val=0;
		vector<int>lst;
		for(int j=0;j<(n-mid);j++)if(i&(1<<j)){
			val+=v[mid+j];
			lst.push_back(mid+j+1);
		}
		auto it=mm.equal_range(s-val);
		for(;it.first!=it.second;++it.first){
			vector<int>x=it.first->second;
			x.insert(x.end(),lst.begin(),lst.end());
			result.push_back(x);
		}
	}
	sort(result.begin(),result.end());
	for(auto &e:result){
		for(int i=0;i<e.size();i++){
			printf(i<e.size()-1?"%d ":"%d\n",e[i]);
		}
	}
}
0