結果

問題 No.15 カタログショッピング
ユーザー myantamyanta
提出日時 2017-05-01 23:48:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 67 ms / 5,000 ms
コード長 1,742 bytes
コンパイル時間 1,941 ms
コンパイル使用メモリ 76,180 KB
実行使用メモリ 11,784 KB
最終ジャッジ日時 2023-10-12 02:33:22
合計ジャッジ時間 2,104 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 55 ms
11,716 KB
testcase_06 AC 63 ms
11,784 KB
testcase_07 AC 62 ms
11,748 KB
testcase_08 AC 67 ms
11,744 KB
testcase_09 AC 65 ms
11,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<vector>
#include<algorithm>
#include<map>


using namespace std;

using dp_t=map<int,int>;
using dp2_t=map<int,vector<int>>;
using p_t=vector<int>;


void solve_r(dp_t& dp, p_t& p, int f, int s, int i, int n)
{
	dp[f]=s;
	if(i<n/2)
	{
		solve_r(dp, p, f, s, i+1, n);
		solve_r(dp, p, f|(1<<(n-1-i)), s+p[i], i+1, n);
	}
}


void solve(dp_t& dp, p_t& p, int n)
{
	solve_r(dp, p, 0, 0, 0, n);
}


void solve2_r(dp2_t& dp, p_t& p, int f, int s, int i, int n, int a)
{
	if(a) dp[s].push_back(f);
	if(i<n)
	{
		solve2_r(dp, p, f, s, i+1, n, 0);
		solve2_r(dp, p, f|(1<<(n-1-i)), s+p[i], i+1, n, 1);
	}
}


void solve2(dp2_t& dp, p_t&p, int n)
{
	solve2_r(dp, p, 0, 0, n/2, n, 1);
}


int main(void)
{
	int n, s, i;
	p_t p;

	while(scanf("%d%d", &n, &s)==2)
	{
		p.resize(n);
		for(i=0;i<n;i++)
		{
			scanf("%d", &p[i]);
		}

		{
			dp_t dph;
			dp2_t dpt;
			solve(dph, p, n);
			solve2(dpt, p, n);
/*
			printf("\n--- dph ---\n");
			for(auto it=dph.begin();it!=dph.end();++it)
			{
				printf("f=%08x  s=%d\n", it->first, it->second);
			}
			printf("--- dpt ---\n");
			for(auto it=dpt.begin();it!=dpt.end();++it)
			{
				printf("s=%d ", it->first);
				for(auto f: it->second)
				{
					printf(" f=%08x", f);
				}
				printf("\n");
			}
*/
			for(auto it=dph.rbegin();it!=dph.rend();++it)
			{
				int hf, hs, tf, f;
				hf=it->first;
				hs=it->second;
				if(dpt.find(s-hs)!=dpt.end())
				{
					vector<int>& v=dpt[s-hs];
					for(auto it=v.rbegin();it!=v.rend();++it)
					{
						tf=*it;
						f=hf|tf;
//						printf("f=%08x\n", f);
						int nc=0;
						for(i=0;i<n;i++)
						{
							if((f>>(n-1-i))&1) printf("%s%d", nc++?" ":"", i+1);
						}
						printf("\n");
					}
				}
			}
		}
	}

	return 0;
}
0