E - ジャンプアクション / Jump Action Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
In a game where you traverse platforms numbered from \(1\) to \(N\) with jumps of distance at most \(K\) to reach platform \(N\), this problem asks you to maximize the total score of the platforms you land on.
By combining dynamic programming (DP) and a double-ended queue (deque), we can find the optimal solution efficiently.
Analysis
1. Formulating Dynamic Programming (DP)
First, given the sequential nature of moving across the platforms, we consider dynamic programming. Let \(DP[i]\) be the maximum score achievable upon reaching platform \(i\) (considering 0-indexed platforms).
If platform \(j\) is the platform visited immediately before platform \(i\), the condition for a valid jump is \(j < i\) and \(X_i - X_j \leq K\). Therefore, the transition equation is as follows:
\[DP[i] = \max_{j < i, \, X_i - X_j \leq K} (DP[j]) + A_i\]
2. Naive Approach and its Limitations
For each platform \(i\), scanning all valid \(j\) with a loop to find the maximum takes \(O(N)\) time in the worst case for a single transition. Since there are \(N\) platforms in total, the overall time complexity is \(O(N^2)\). With the constraint \(N \leq 2 \times 10^5\), the number of operations would reach roughly \(4 \times 10^{10}\), exceeding the execution time limit and resulting in TLE (Time Limit Exceeded).
3. Optimization using Sliding Window Maximum
The coordinates \(X\) of the platforms are strictly monotonically increasing (\(X_0 < X_1 < \dots < X_{N-1}\)). Therefore, as platform \(i\) moves to the right, the left boundary of the valid range of source platforms \(j\) (\(X_j \geq X_i - K\)) also moves to the right.
This can be rephrased as the problem of “finding the maximum \(DP\) value within a sliding window (Sliding Window Maximum).”
By maintaining a monotonic queue using a double-ended queue (deque), we can retrieve the maximum value in amortized \(O(1)\) time.
Algorithm
Initialization:
- Initialize the \(DP\) array with a sufficiently small value (\(-\infty\)).
- Set the score for the starting platform \(0\): \(DP[0] = A_0\).
- Prepare a double-ended queue
dqto store indices, and add0as the initial state. - Maintain
dqsuch that its elements are always in descending order of their \(DP\) values. This ensures that the front of the queue (dq.front()) always holds the index with the maximum value within the jumpable range.
Transitions to each platform (\(i = 1, \dots, N-1\)):
- Remove out-of-range elements: While the distance from platform \(j\) at the front of the queue to platform \(i\) exceeds \(K\) (\(X_i - X_j > K\)), remove elements from the front of the queue.
- Update \(DP\) value: If the queue is not empty, the platform at the front is the optimal jump source. Update \(DP[i] = DP[\text{front}] + A_i\). (If the queue is empty, it means platform \(i\) cannot be reached from any platform.)
- Add to queue: Before adding platform \(i\) to the queue, remove all platforms from the back of the queue whose \(DP\) values are less than or equal to \(DP[i]\). This is because those platforms are to the left of platform \(i\) and have scores no greater than \(DP[i]\), meaning they have no chance of being selected as the maximum in the future. After that, add platform \(i\) to the back of the queue.
Outputting the answer:
- Finally, if \(DP[N-1]\) remains at its initial value (unreachable), output
-1. - If it is reachable, output the value of \(DP[N-1]\).
- Finally, if \(DP[N-1]\) remains at its initial value (unreachable), output
Complexity
- Time Complexity: \(O(N)\) Each platform is pushed to the queue at most \(1\) time and popped at most \(1\) time. Throughout the entire loop, the total number of queue operations is at most \(2N\), so the amortized time complexity is \(O(N)\), which easily passes well within the time limit.
- Space Complexity: \(O(N)\) We use \(O(N)\) memory to store the platform data, the DP table, and the queue.
Key Implementation Points
Preventing Overflow: The total score can become extremely large in both the positive and negative directions. Since it can reach up to approximately \(2 \times 10^5 \times 10^9 = 2 \times 10^{14}\), it will overflow a 32-bit integer type. Use a 64-bit integer type (
long longin C++) for the scores and the DP table.Magnitude of the Initial Value (\(-\infty\)): Because scores \(A_i\) can be negative, setting a moderately small value (such as \(-10^9\)) as \(-\infty\) can make it indistinguishable from an unreachable state when negative scores are added. Make sure to set the initial value to a sufficiently small number (e.g., \(-4 \times 10^{18}\)).
Source Code
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
long long k;
if (!(cin >> n >> k)) return 0;
vector<long long> x(n);
vector<long long> a(n);
for (int i = 0; i < n; ++i) {
cin >> x[i] >> a[i];
}
const long long INF = 4e18;
vector<long long> dp(n, -INF);
dp[0] = a[0];
deque<int> dq;
dq.push_back(0);
for (int i = 1; i < n; ++i) {
while (!dq.empty() && x[i] - x[dq.front()] > k) {
dq.pop_front();
}
if (!dq.empty()) {
dp[i] = dp[dq.front()] + a[i];
while (!dq.empty() && dp[dq.back()] <= dp[i]) {
dq.pop_back();
}
dq.push_back(i);
}
}
if (dp[n - 1] <= -INF / 2) {
cout << -1 << "\n";
} else {
cout << dp[n - 1] << "\n";
}
return 0;
}
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: