結果

問題 No.15 カタログショッピング
ユーザー masamasa
提出日時 2015-01-22 15:32:41
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,958 bytes
コンパイル時間 788 ms
コンパイル使用メモリ 86,192 KB
実行使用メモリ 18,380 KB
最終ジャッジ日時 2023-09-05 03:35:55
合計ジャッジ時間 7,390 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <map>
#include <bitset>

using namespace std;

const int n_max = 31;
const int n_mid = 16;

int main() {
	int n, s;

	cin >> n >> s;

	vector<int> p(n, 0);
	for (int i = 0; i < n; i++) {
		cin >> p[i];
	}

	vector< map<int, vector<int> > > maps(2); // (price, vector<bits>)のvector
	maps[0][0] = vector<int>(1,0);
	maps[1][0] = vector<int>(1,0);

	for (int idx = 0; idx < 2; idx++) {
		int n_bit, offset;
		if (idx == 0) {
			n_bit = min(n, n_mid);
			offset = 0;
		} else {
			n_bit = max(0, n - n_mid);
			offset = n_mid;
		}


		for (int i = 1; i < (1 << n_bit); i++) {
			int total = 0;
			for (int j = 0; j < n_bit; j++) {
				if (i & (1 << j)) {
					total += p[j + offset];
				}
			}
			if (total > s) {
				continue;
			}
			if (maps[idx].count(total)) {
				maps[idx][total].push_back(i);
			} else {
				maps[idx][total] = vector<int>(1, i);
			}
		}
	}

	vector<int> ans;
	for (auto it0 = maps[0].begin(); it0 != maps[0].end(); it0++) {
		for (auto it1 = maps[1].begin(); it1 != maps[1].end(); it1++) {
			if(it0->first + it1->first == s) {
				for (int i = 0; i < it0->second.size(); i++) {
					for (int j = 0; j < it1->second.size(); j++) {
						ans.push_back(it0->second[i] | (it1->second[j] << n_mid) );
					}
				}
			}
		}
	}

	for (int i = 0; i < ans.size(); i++) {
		int tmp = 0;
		// cout << static_cast< bitset<n_max> >(ans[i]) << endl;

		for (int j = 0; j < n_max; j++) {
			if (ans[i] & (1 << j)) {
				tmp |= 1 << (n_max - j - 1);
			}
		}
		ans[i] = tmp;
		// cout << static_cast< bitset<n_max> >(tmp) << endl;
	}

	sort(ans.begin(), ans.end(), greater<int>());
	for (int i = 0; i < ans.size(); i++) {
		bool first = true;
		for (int j = n_max - 1; j >= 0; j--) {
			if (ans[i] & (1 << j)) {
				if (!first) {
					printf(" ");
				}
				printf("%d", n_max - j);
				first = false;
			}
		}
		printf("\n");
	}
	return 0;
}
0