結果

問題 No.15 カタログショッピング
ユーザー OnjuOnju
提出日時 2015-02-12 20:53:40
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,291 bytes
コンパイル時間 1,136 ms
コンパイル使用メモリ 76,824 KB
実行使用メモリ 4,500 KB
最終ジャッジ日時 2023-09-06 00:09:53
合計ジャッジ時間 3,579 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <bitset>
#include <vector>
#include <string>
#include <sstream>
#define setBit(x,b) ((x)|(1<<(b)))
#define getBit(x,b) ((x)&(1<<(b)))
#define N_MAX 31
#define S_MAX 310000000
#define P_MAX 10000000
using namespace std;

typedef struct
{
	int no;
	int P;
}Goods;

int N, S;
Goods G[N_MAX];
vector<string> ans;
int max_p[N_MAX];

bool compG(const Goods& x, const Goods& y)
{
	return x.P > y.P;
}

void DP(int t, int n, int op)
{
	if (t == 0 && n >= N)
	{
		bool first = true;
		stringstream str;

		for (int i = 0; i < N; ++i)
		{
			if (getBit(op, i))
			{
				if (!first)
					str << " ";
				str << i;
				first = false;
			}
		}

		ans.push_back(str.str());
		return;
	}

	if (t < 0 || n >= N || max_p[n] < t)
		return;

	DP(t - G[n].P, n + 1, setBit(op, G[n].no));
	DP(t, n + 1, op);
}

void calc()
{
	sort(G, G + N, compG);

	max_p[N - 1] = G[N - 1].P;
	for (int i = N - 2; i >= 0; --i)
	{
		max_p[i] = max_p[i + 1] + G[i].P;
	}

	DP(S, 0, 0);

	sort(ans.begin(), ans.end());
}

int main()
{
	cin >> N >> S;

	for (int i = 0; i < N; ++i)
	{
		G[i].no = i + 1;
		cin >> G[i].P;
	}

	calc();

	for (vector<string>::iterator it = ans.begin(); it != ans.end(); ++it)
		cout << *it << endl;

	return 0;
}
0