E - ジャンプアクション / Jump Action Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
We use dynamic programming to find the maximum score when moving from platform \(1\) to each platform. By maintaining the candidate platforms to jump from using a monotonic queue (deque), the overall process can be done in \(O(N)\) time.
Analysis
Considering Dynamic Programming
Let \(dp_i\) be “the maximum total score when reaching platform \(i\) from platform \(1\).”
Since we also obtain the score of the starting point, the initial value is
\(dp_1 = A_1\).
A platform \(j\) from which we can jump to platform \(i\) must satisfy \(j < i\) and \(X_i - X_j \leq K\). Therefore, the transition is
\(dp_i = A_i + \max_{\substack{j<i \\ X_i-X_j\leq K}} dp_j\).
Note that the maximum is taken only over platforms reachable from platform \(1\); if there are no candidates, platform \(i\) cannot be reached.
If we implement this formula naively by checking all previous platforms for each platform, the time complexity will be \(O(N^2)\). This is too slow to pass within the time limit for \(N \leq 2 \times 10^5\).
Eliminating Unnecessary Candidates
The platforms are sorted in ascending order of their coordinates. As we move to the right, new platforms are added as jump candidates, and older platforms that become too far away are removed from the candidates.
Furthermore, we can utilize the following property:
For an already processed platform \(j\) and a newly processed platform \(i\), if
- \(j < i\)
- \(dp_j \leq dp_i\)
hold, then there is no need to keep platform \(j\) as a candidate for future jumps.
This is because for any future platform \(t\), if it is possible to jump from platform \(j\), it is also possible to jump from platform \(i\), which is located further to the right:
\(X_t-X_i < X_t-X_j \leq K\)
Moreover, the maximum score up to platform \(i\) is greater than or equal to that of platform \(j\). Therefore, using platform \(i\) will always be at least as good as using platform \(j\).
For example, if the maximum score up to an older platform is \(5\) and the maximum score up to a newer platform is \(8\), there is no advantage in choosing the older platform in the future.
Using this property, we maintain the candidates in a double-ended queue (deque) such that the coordinates are in ascending order and the maximum scores are in strictly descending order. Then, we can obtain the required maximum simply by looking at the front of the queue.
Algorithm
We store candidates as pairs \((X_j, dp_j)\) in a deque dq. We process the platforms from left to right.
1. Initialize platform \(1\)
Set \(dp_1 = A_1\) and push \((X_1, dp_1)\) into the queue.
2. Remove candidates that can no longer be jumped from
When processing platform \(i\), while platform \(j\) at the front of the queue satisfies
\(X_i-X_j>K\),
we pop from the front.
Since the coordinates in the queue are in ascending order, candidates that are too far away can be removed sequentially from the front.
3. Calculate the maximum score up to platform \(i\)
If the queue is not empty, the maximum score of the candidate at the front is the optimal value to transition to platform \(i\).
Therefore, we set
\(dp_i = \text{maximum score at the front of the queue} + A_i\).
If the queue is empty, platform \(i\) cannot be reached. In this case, we can immediately print \(-1\) and terminate.
This is because if no reachable platform to the left of platform \(i\) can reach platform \(i\), none of them can reach any platform further to the right either. It is impossible to jump past platform \(i\) to proceed further.
4. Remove candidates inferior to the new platform
While the maximum score of the candidate at the back of the queue is less than or equal to \(dp_i\), we pop from the back.
These candidates are no longer needed because platform \(i\) can be used in their place from now on.
Finally, we push \((X_i, dp_i)\) to the back. This maintains the invariant that the maximum scores in the queue are in strictly descending order from front to back.
Once all platforms up to platform \(N\) have been processed, output \(dp_N\).
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(N)\)
Each platform is pushed into the queue exactly once and popped at most once. Therefore, the total number of operations performed by the while loops is also \(O(N)\).
Implementation Notes
Use a 64-bit integer for the total score.
The absolute value of the total score can reach up to around \(N \times 10^9 \leq 2 \times 10^{14}\), solong longis used in the reference code.Handle negative scores as they are.
The scores of the start and goal platforms must always be added. In addition, it may be necessary to land on platforms with negative scores to continue moving, so you should not arbitrarily compare or clamp the maximum score with \(0\).A jump is valid if the distance is exactly \(K\).
The condition to pop from the queue is when the distance is strictly greater than \(K\), not greater than or equal to \(K\).Remove older candidates even if their scores are equal.
Since the newer platform is located further to the right, it can serve as a valid jump origin for longer in the future; hence, the condition for popping from the back uses<=.Processing can be done on the fly while reading input.
Since past information is maintained entirely within the queue, there is no need to store all elements in coordinate/score arrays or keep the entire \(dp\) array in memory.Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
long long K;
cin >> N >> K;
deque<pair<long long, long long>> dq;
long long answer = 0;
for (int i = 0; i < N; ++i) {
long long x, a;
cin >> x >> a;
if (i == 0) {
answer = a;
} else {
while (!dq.empty() && x - dq.front().first > K) {
dq.pop_front();
}
if (dq.empty()) {
cout << -1 << '\n';
return 0;
}
answer = dq.front().second + a;
}
while (!dq.empty() && dq.back().second <= answer) {
dq.pop_back();
}
dq.emplace_back(x, answer);
}
cout << answer << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.
posted:
last update: