結果

問題 No.3329 Only the Rightest Choice is Right!!!
コンテスト
ユーザー elphe
提出日時 2025-08-11 11:48:58
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 2,591 bytes
コンパイル時間 2,707 ms
コンパイル使用メモリ 286,284 KB
実行使用メモリ 11,300 KB
最終ジャッジ日時 2025-09-09 17:11:54
合計ジャッジ時間 14,399 ms
ジャッジサーバーID
(参考情報)
judge / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 7 TLE * 1 -- * 66
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

static inline constexpr std::vector<uint_fast32_t> solve(const uint_fast32_t N, const uint_fast32_t W, const std::vector<uint_fast32_t>& v, const std::vector<uint_fast32_t>& w) noexcept
{
	uint_fast32_t best_value = 0;
	std::vector<uint_fast32_t> ans, cur;
	ans.reserve(N), cur.reserve(N);

	using Compare = decltype([](const std::pair<uint_fast32_t, uint_fast32_t>& a, const std::pair<uint_fast32_t, uint_fast32_t>& b) { return static_cast<uint_fast64_t>(a.first) * b.second > static_cast<uint_fast64_t>(b.first) * a.second; });
	std::multiset<std::pair<uint_fast32_t, uint_fast32_t>, Compare> rest_items;
	for (uint_fast32_t i = 0; i != N; ++i)
		rest_items.insert({ v[i], w[i] });

	const auto dfs = [&](const auto self, const uint_fast32_t cur_pos = 0, const uint_fast32_t total_value = 0, const uint_fast32_t total_weight = 0) constexpr noexcept -> void
		{
			if (total_weight > W) return;
			else if (cur_pos == N && total_value > best_value)
			{
				ans = cur, best_value = total_value;
				return;
			}

			uint_fast32_t add_value = 0, add_weight = 0;
			std::multiset<std::pair<uint_fast32_t, uint_fast32_t>, Compare>::iterator itr;
			for (itr = rest_items.begin(); itr != rest_items.end() && add_weight + itr->second <= W - total_weight; ++itr)
				add_value += itr->first, add_weight += itr->second;

			if (itr != rest_items.end())
			{
				if (total_value + add_value + itr->first * (W - total_weight - add_weight) / static_cast<double>(itr->second) > best_value)
				{
					rest_items.erase(rest_items.find({ v[cur_pos], w[cur_pos] }));
					self(self, cur_pos + 1, total_value, total_weight);
					cur.push_back(cur_pos + 1);
					self(self, cur_pos + 1, total_value + v[cur_pos], total_weight + w[cur_pos]);
					cur.pop_back();
					rest_items.insert({ v[cur_pos], w[cur_pos] });
				}
			}
			else
			{
				if (total_value + add_value > best_value)
				{
					ans = cur, best_value = total_value + add_value;
					for (uint_fast32_t i = cur_pos + 1; i <= N; ++i)
						ans.push_back(i);
				}
			}
		};

	dfs(dfs);
	return ans;
}

static inline void output(const std::vector<uint_fast32_t>& ans) noexcept
{
	std::cout << ans.size() << '\n' << ans[0];
	for (uint_fast32_t i = 1; i != ans.size(); ++i)
		std::cout << ' ' << ans[i];
	std::cout << '\n';
}

int main()
{
	std::cin.tie(nullptr);
	std::ios::sync_with_stdio(false);

	uint_fast32_t N, W, i;
	std::cin >> N >> W;
	std::vector<uint_fast32_t> v(N), w(N);
	for (i = 0; i != N; ++i)
		std::cin >> v[i];
	for (i = 0; i != N; ++i)
		std::cin >> w[i];

	output(solve(N, W, v, w));
	return 0;
}
0