From f8e979f1149aef6889a1ea1f830f7112d5bb06ed Mon Sep 17 00:00:00 2001 From: subcrip Date: Wed, 29 May 2024 19:17:38 +0800 Subject: [PATCH] Add dp/knapsack.cc Signed-off-by: subcrip --- dp/knapsack.cc | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 dp/knapsack.cc diff --git a/dp/knapsack.cc b/dp/knapsack.cc new file mode 100644 index 0000000..be7c5d0 --- /dev/null +++ b/dp/knapsack.cc @@ -0,0 +1,28 @@ +// Part of the code from a Cf problem to demonstrate how to dump the path from a knapsack-dp solution. +for (int j = 1; j <= m; ++j) { + auto [t, p, idx] = events[i][j - 1]; + for (int k = 0; k <= 100; ++k) { + dp[j][k] = dp[j - 1][k]; + } + for (int k = 0; k <= 100; ++k) { + int new_percent = min(100, dp[j - 1][k].second + p); // In this problem we just omit the capacity over 100. + if (dp[j][new_percent].first > dp[j - 1][k].first + t) { + dp[j][new_percent] = {dp[j - 1][k].first + t, dp[j - 1][k].second + p}; + } + } +} +ll tm = dp[m][100].first; +if (tm + start > a[i]) { + cout << -1 << '\n'; + return; +} else { + int k = dp[m][100].second; + for (int j = m; j >= 1; --j) { + auto [t, p, idx] = events[i][j - 1]; + if (dp[j][min(100, k)] < dp[j - 1][min(100, k)]) { // Find the last step of the construction process. + k -= p; // Prepare to find the next one. + res.emplace_back(idx); // Add the current item as part of the solution. + } + } + start += tm; +}