結果

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

ソースコード

diff #
raw source code

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

// アルゴリズム
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
{
	// 動的計画法パート
	std::vector<std::vector<uint_fast64_t>> dp_value(N + 1, std::vector<uint_fast64_t>(W + 1, 0));
	std::vector<std::vector<uint_fast32_t>> dp_next(N + 1, std::vector<uint_fast32_t>(W + 1, UINT_FAST32_MAX));
	// 後ろから見ていく
	for (int_fast32_t i = N - 1; i >= 0; --i)
	{
		for (uint_fast32_t j = 0; j != w[i]; ++j)
			dp_value[i][j] = dp_value[i + 1][j], dp_next[i][j] = dp_next[i + 1][j];
		for (uint_fast32_t j = w[i]; j <= W; ++j)
		{
			// ご利益が等しいなら、選ばない方が良い
			if (dp_value[i + 1][j] >= dp_value[i + 1][j - w[i]] + v[i])
				dp_value[i][j] = dp_value[i + 1][j], dp_next[i][j] = dp_next[i + 1][j];
			else
				dp_value[i][j] = dp_value[i + 1][j - w[i]] + v[i], dp_next[i][j] = i + 1 ;
		}
	}

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

	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);

	uint32_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