E - ジャンプアクション / Jump Action Editorial by admin Editorial for printing
Gemini 3.8 Flash (High)Overview
This problem asks us to find the maximum score achievable by jumping along platforms on a number line, starting at platform \(1\) and reaching platform \(N\). Each jump can cover a distance of at most \(K\).
Analysis
1. Applying Dynamic Programming (DP)
Let dp[j] be the maximum score when reaching platform \(j\).
Since we start at platform \(1\), we set dp[0] = A[1] (in 0-indexed terms) and initialize unreachable platforms to \(-\infty\).
We can directly jump to platform \(j\) from any reachable platform \(i\) that satisfies \(i < j\) and \(X_j - X_i \leq K\). Therefore, the transition is as follows:
\[dp[j] = \max_{\substack{i < j \\ X_j - X_i \leq K}} (dp[i]) + A_j\]
2. Issues with the Naive Approach
For each \(j\), if we perform a linear search over all \(i\) from \(0\) to \(j - 1\) each time to find the maximum value among those satisfying the condition, each transition takes \(O(N)\) in the worst case. The overall time complexity would be \(O(N^2)\). For \(N \leq 2 \times 10^5\), this requires roughly \(4 \times 10^{10}\) operations, which will exceed the time limit (TLE).
3. Optimization using a Sliding Window
The platform coordinates are sorted in ascending order: \(X_1 < X_2 < \dots < X_N\). Rearranging the condition \(X_j - X_i \leq K\) gives \(X_i \geq X_j - K\). As \(j\) increases, the lower bound \(X_j - K\) for valid transition sources also increases monotonically.
This is the Sliding Window Maximum problem, where we slide a window and query the maximum value within that range.
By utilizing a monotonic queue (double-ended queue: deque), we can efficiently find the maximum dp[i] within the valid range in amortized \(O(1)\) time.
Algorithm
We maintain candidate transition indices using a double-ended queue (deque).
The elements inside the queue are maintained such that their dp values are always in descending order (with the maximum value at the front).
Initialization:
- Set all elements in the
dparray to \(-\infty\), and setdp[0] = A[0]. - Push the starting index
0into the queueq.
- Set all elements in the
Processing Each Platform (\(j = 1, 2, \dots, N-1\)):
- Remove Out-of-Range Elements:
- As long as the platform \(i\) at the front of the queue satisfies \(X_i < X_j - K\), a jump cannot reach it, so remove it from the front of the queue (
popleft).
- As long as the platform \(i\) at the front of the queue satisfies \(X_i < X_j - K\), a jump cannot reach it, so remove it from the front of the queue (
- Update the DP Value:
- If the queue is not empty, the platform at the front holds the maximum
dpvalue within the reachable jump range. - Update
dp[j]asdp[j] = dp[q[0]] + A[j].
- If the queue is not empty, the platform at the front holds the maximum
- Add the New Candidate:
- For the platform \(k\) at the back of the queue, remove it from the back (
pop) as long asdp[k] <= dp[j]. - (This is because \(j\) is further to the right than \(k\) and has an equal or higher score, meaning \(k\) can never be the optimal choice for any future transition.)
- Afterwards, push \(j\) to the back of the queue.
- For the platform \(k\) at the back of the queue, remove it from the back (
- Remove Out-of-Range Elements:
Determining the Answer:
- If
dp[N-1]remains \(-\infty\) in the end, platform \(N\) is unreachable, so output-1. - If it is reachable, output
dp[N-1].
- If
Complexity
- Time Complexity: \(O(N)\)
- Each platform index is pushed to the queue at most once and popped at most once.
- Thus, the total number of queue operations across the entire loop is \(O(N)\), allowing the algorithm to run in \(O(N)\) overall.
- Space Complexity: \(O(N)\)
- \(O(N)\) memory is used to store the platform coordinates, scores, the
dparray, and the queue.
- \(O(N)\) memory is used to store the platform coordinates, scores, the
Implementation Notes
Handling Unreachable Platforms: If there are no valid platforms from which platform \(j\) can be reached, the queue becomes empty. In this case, skipping the update of
dp[j]and avoiding adding it to the queue prevents invalid transitions through unreachable platforms.Fast I/O: Since the input size is large (\(N \leq 2 \times 10^5\)), reading all input at once using
sys.stdin.read().split()in Python can significantly reduce execution time.Source Code
from collections import deque
import sys
def main():
input_data = sys.stdin.read().split()
if not input_data:
return
N = int(input_data[0])
K = int(input_data[1])
X = [0] * N
A = [0] * N
idx = 2
for i in range(N):
X[i] = int(input_data[idx])
A[i] = int(input_data[idx + 1])
idx += 2
dp = [-float("inf")] * N
dp[0] = A[0]
q = deque([0])
for j in range(1, N):
x_limit = X[j] - K
while q and X[q[0]] < x_limit:
q.popleft()
if q:
dp_j = dp[q[0]] + A[j]
dp[j] = dp_j
while q and dp[q[-1]] <= dp_j:
q.pop()
q.append(j)
if dp[N - 1] == -float("inf"):
print(-1)
else:
print(dp[N - 1])
if __name__ == "__main__":
main()
This editorial was generated by gemini-3.8-flash-high.
posted:
last update: