結果

問題 No.3329 Only the Rightest Choice is Right!!!
コンテスト
ユーザー elphe
提出日時 2025-08-07 15:22:37
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 131 ms / 1,571 ms
コード長 1,692 bytes
コンパイル時間 1,131 ms
コンパイル使用メモリ 111,988 KB
実行使用メモリ 144,000 KB
最終ジャッジ日時 2025-11-02 16:29:07
合計ジャッジ時間 7,183 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 74
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdint>
#include <vector>
#include <cmath>

// アルゴリズム
static inline constexpr std::vector<uint_least32_t> solve(const uint_least32_t N, const uint_least32_t W, const std::vector<uint_least32_t>& v, const std::vector<uint_least32_t>& w) noexcept
{
	// 動的計画法パート
	std::vector<std::vector<std::pair<uint_least64_t, uint_least32_t>>> dp(N + 1, std::vector<std::pair<uint_least64_t, uint_least32_t>>(W + 1, { 0, UINT_LEAST32_MAX }));
	// 後ろから見ていく
	for (int_least32_t i = N - 1; i >= 0; --i)
	{
		for (uint_least32_t j = 0; j != w[i]; ++j)
			dp[i][j] = dp[i + 1][j];
		for (uint_least32_t j = w[i]; j <= W; ++j)
		{
			// ご利益が等しいなら、選ばない方が良い
			if (dp[i + 1][j].first >= dp[i + 1][j - w[i]].first + v[i])
				dp[i][j] = dp[i + 1][j];
			else
				dp[i][j] = { dp[i + 1][j - w[i]].first + v[i], i + 1 };
		}
	}

	// トレースバック(遡及)パート
	std::vector<uint_least32_t> ans;
	for (uint_least32_t i = dp[0][W].second, budget = W; i != UINT_LEAST32_MAX; budget -= w[i - 1], i = dp[i][budget].second)
		ans.push_back(i);

	return ans;
}

// 出力
static inline void output(const std::vector<uint_least32_t>& ans) noexcept
{
	std::cout << ans.size() << '\n' << ans[0];
	for (uint_least32_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_least32_t N, W, i;
	std::cin >> N >> W;
	std::vector<uint_least32_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