E - ジャンプアクション / Jump Action Editorial by admin Editorial for printing
gpt-6-astra-mediumSummary
We find the maximum score when moving from platform \(1\) to each platform using dynamic programming. By maintaining the maximum transition value with a monotonic deque (double-ended queue), we can process the entire problem in \(O(N)\) time.
Analysis
1. Dynamic Programming Formulation
Let \(dp_i\) be “the maximum total score when reaching platform \(i\) from platform \(1\).”
Since we also collect the score of the starting platform, the initial value is:
\(dp_1 = A_1\)
If the platform visited immediately before platform \(i\) is \(j\), it is required that \(j < i\) and \(X_i-X_j \leq K\). Therefore, over all reachable platforms \(j\), we can compute:
\(dp_i = A_i + \max_{\substack{j<i \\ X_j \geq X_i-K}} dp_j\)
Because some platforms have negative scores, landing on every single platform is not necessarily optimal. We need to track the maximum score up to each platform.
2. Checking All Transitions Every Time is Too Slow
For each \(i\), checking all previous platforms takes \(O(N^2)\) time in total. This will not run within the time limit for \(N \leq 2 \times 10^5\).
All we need is the maximum \(dp\) value among platforms that satisfy the condition. Furthermore, as we process platforms from left to right, the lower bound of coordinates that can be jumped from, \(X_i-K\), also moves to the right.
Using this property, we can maintain the candidates using a deque.
3. Eliminating Candidates That Will Not Be Needed in the Future
Suppose that for already processed platforms \(p<q\), the following holds:
\(dp_p \leq dp_q\)
For any future platform \(i>q\), if we can jump from platform \(p\), we can also jump from platform \(q\), which is further to the right. Moreover, the score up to platform \(q\) is greater than or equal to that of platform \(p\).
In other words, we will never need to use platform \(p\) for future transitions.
For example, suppose the candidate pairs of (coordinate, maximum score) currently in the deque are:
\((2,10), (5,7)\)
If a new candidate \((8,9)\) is added, \((5,7)\) is to the left of the new candidate and has a lower score, so it can be removed. On the other hand, \((2,10)\) is kept because its score is higher.
By removing unnecessary candidates in this way, the deque maintains the following order:
- Coordinates increase from front to back.
- Maximum scores are strictly monotonically decreasing from front to back.
Therefore, after removing candidates that are out of range, the front of the deque is always the optimal transition source.
Algorithm
The deque stores candidate platforms as pairs of \((X_j, dp_j)\).
For platform \(1\), set \(dp_1=A_1\) and push \((X_1, A_1)\) into the deque.
Process platforms \(i=2,3,\dots,N\) in order:
① Remove candidates outside the jump range
While the coordinate at the front of the deque is strictly less than \(X_i-K\), pop from the front.
Since coordinates are in ascending order, out-of-range candidates can be removed from the front. Additionally, a platform that falls out of range once can never be used for platforms further to the right either.
② Check reachability
If the deque is empty, platform \(i\) cannot be reached. In this case, output \(-1\) and terminate.
If no candidate can reach platform \(i\), then platforms further to the right cannot be reached either. Candidates removed due to score comparisons can always be replaced by candidates to their right with equal or better scores, so pruning candidates never results in false unreachability.
③ Compute the maximum score
Since the front of the deque holds the maximum \(dp\) value, compute:
\(dp_i = \text{maximum score at the front of the deque} + A_i\)
④ Remove candidates worse than the new candidate
While the maximum score at the back of the deque is less than or equal to \(dp_i\), pop from the back.
These candidates are to the left of platform \(i\) and do not have a higher score, so they will never need to be used in the future.
⑤ Add the new candidate
Push \((X_i, dp_i)\) to the back of the deque.
- Output the finally computed \(dp_N\).
Complexity
- Time complexity: \(O(N)\)
- Space complexity: \(O(N)\)
Each platform is added to the deque exactly once and removed at most once. Therefore, even with the while loops, the total number of push and pop operations is \(O(N)\).
Implementation Notes
Jumps of distance exactly \(K\) are valid.
The out-of-range condition must becoordinate < x - k. If you use<=, candidates at a distance of exactly \(K\) would be incorrectly removed.If maximum scores are equal, keep the rightmost platform.
Platforms further to the right have a wider reachable range for future jumps, so the condition to remove from the back can simply bemax_score <= score.A negative score does not mean unreachable.
Reachability is determined by whether the deque is empty, not by the sign or value of the score.The answer is \(dp_N\).
Output thescorecomputed for the last platform, not the maximum value across the entire deque.The total score does not fit in a 32-bit integer.
The absolute value can reach around \(2 \times 10^{14}\), so languages with fixed-width integers require a 64-bit integer type. Python handles this automatically with arbitrary-precision integers.Source Code
import sys
from collections import deque
def main():
readline = sys.stdin.buffer.readline
n, k = map(int, readline().split())
x, score = map(int, readline().split())
dq = deque([(x, score)])
for _ in range(n - 1):
x, a = map(int, readline().split())
limit = x - k
while dq and dq[0][0] < limit:
dq.popleft()
if not dq:
print(-1)
return
score = dq[0][1] + a
while dq and dq[-1][1] <= score:
dq.pop()
dq.append((x, score))
print(score)
if __name__ == "__main__":
main()
This editorial was generated by gpt-6-astra-medium.
posted:
last update: