F - ダンジョン攻略 / Dungeon Conquest Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
In a dungeon where the maximum health is \(M\), this problem asks for the maximum number of coins that can be obtained while clearing up to Room \(N\), choosing either to “restore health” or “gain coins” at each resting place. This can be solved using dynamic programming (DP).
Analysis
Naive Approach and Its Issues
At each resting place (\(N - 1\) locations in total), there are \(2\) choices: “drink water” or “open a treasure chest”. If we simply try to explore all action patterns, the number of combinations becomes \(2^{N-1}\), which is an astronomical number under the constraint \(N \le 3000\), resulting in Time Limit Exceeded (TLE).
Merging States (Applying DP)
Observing the state immediately after clearing each room, the only factor that affects subsequent progression is the “current health”. Since the health value can only be an integer between \(0\) and \(M\) inclusive, there are at most \(M + 1\) possible health values.
Therefore, even if there are multiple paths that result in the same health \(h\) immediately after clearing the same room, it is sufficient to keep track only of the one with the maximum total number of coins collected.
This is a classic scenario where dynamic programming (DP) is extremely effective.
Algorithm
We perform DP using the health immediately after clearing Room \(i\) as the state.
DP Table Definition
dp[h]: The maximum number of coins collected when the health immediately after clearing the current room is \(h\) (unreachable states are set to \(-1\)).
1. Initialization
Before entering the dungeon, the health is \(M\), and we first clear Room \(1\).
Since clearing Room \(1\) reduces health by \(A_1\), we initialize:
- dp[M - A_1] = 0
- dp[h] = -1 for all other \(h\)
2. State Transitions (Processing Resting Place \(i\) and Room \(i + 1\))
For \(i = 1, 2, \dots, N-1\), we update the DP table in the following \(2\) steps in order:
- Action at resting place \(i\):
For each current health \(h\) (
dp[h] != -1):- If drinking spring water: The health after recovery becomes \(h' = \min(M, h + B_i)\), with no increase in coins.
- If opening a treasure chest: The health remains \(h' = h\), and the number of coins increases by \(C_i\).
We create a temporary array next_dp reflecting these actions.
- Clearing Room \(i + 1\):
To clear Room \(i + 1\), health of at least \(A_{i+1}\) is required.
- When \(h' \ge A_{i+1}\), the health after clearing becomes \(h' - A_{i+1}\).
- When \(h' < A_{i+1}\), the room cannot be cleared due to insufficient health, so this state is discarded.
We set this result as the new dp array.
3. Calculating the Answer
The answer is the maximum value in the dp array after clearing Room \(N\) (since it is guaranteed that the dungeon can be cleared with appropriate choices, at least \(1\) valid state will exist).
Complexity
- Time Complexity: \(O(NM)\)
- There are \(N - 1\) steps, and in each step, states with health \(0 \le h \le M\) are updated in \(O(1)\).
- Given \(N, M \le 3000\), the number of operations is at most about \(3000 \times 3000 = 9 \times 10^6\), which easily runs within the time limit.
- Space Complexity: \(O(M)\)
- By maintaining and sequentially updating only the states from the previous room (an array of size \(M + 1\)), the space complexity is kept to \(O(M)\).
Implementation Notes
Handling Overflow: The value of coins \(C_i\) is up to \(10^9\), so if coins are collected at all \(N - 1\) resting places, the total can reach up to approximately \(3000 \times 10^9 = 3 \times 10^{12}\). Since this exceeds the maximum value of a 32-bit signed integer (approx. \(2 \times 10^9\)), a 64-bit integer type (
long longin C++) must be used for the DP array managing coin counts and for the answer variable.Maximum Health Cap: Do not forget to cap the health with \(\min(M, h + B_i)\) when drinking water so that it does not exceed the maximum value \(M\).
Source Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
if (!(cin >> n >> m)) return 0;
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<long long> b(n - 1), c(n - 1);
for (int i = 0; i < n - 1; ++i) {
cin >> b[i] >> c[i];
}
// dp[h]: 体力が h のときのコインの最大値 (-1 は到達不能)
vector<long long> dp(m + 1, -1);
dp[m - a[0]] = 0;
for (int i = 0; i < n - 1; ++i) {
// 休憩所 i での行動
vector<long long> next_dp(m + 1, -1);
for (int h = 0; h <= m; ++h) {
if (dp[h] == -1) continue;
// 行動 1: 泉の水を飲む
int h_water = min((long long)m, h + b[i]);
next_dp[h_water] = max(next_dp[h_water], dp[h]);
// 行動 2: 宝箱を開ける
next_dp[h] = max(next_dp[h], dp[h] + c[i]);
}
// 部屋 i + 1 の攻略
fill(dp.begin(), dp.end(), -1);
for (int h = a[i + 1]; h <= m; ++h) {
if (next_dp[h] != -1) {
dp[h - a[i + 1]] = max(dp[h - a[i + 1]], next_dp[h]);
}
}
}
long long ans = -1;
for (int h = 0; h <= m; ++h) {
ans = max(ans, dp[h]);
}
cout << ans << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: