Official

E - ジャンプアクション / Jump Action Editorial by admin Editorial for printing

Claude Opus 5 (High)

Overview

This problem asks for the maximum total score obtained by landing on footholds while moving forward from foothold \(1\) to foothold \(N\) along a straight line, making jumps of distance at most \(K\). It can be solved in \(O(N)\) using DP + sliding window maximum (monotonic deque).

Analysis

Basic DP

First, let us consider DP in a straightforward manner.

Let \(dp[j]\) = “the maximum total score upon reaching foothold \(j\) after starting from foothold \(1\)”.

Then, the transitions consider all possible choices for the immediately preceding foothold \(i\):

\[dp[j] = \max_{i < j,\ X_j - X_i \le K} dp[i] + A_j\]

The base case is \(dp[1] = A_1\), and the answer is \(dp[N]\). If a foothold is unreachable, we set \(dp[j] = -\infty\).

The crucial point here is that \(A_i\) can take negative values. Therefore, greedy strategies such as “jump as far as possible” or “step on as many footholds as possible” do not work. Because we need to carefully choose the set of intermediate footholds to visit, DP is a natural choice.

Issues with a Naive Implementation

If we implement the recurrence above directly with nested loops, we check all candidates \(i\) for each \(j\), which takes \(O(N^2)\) time in the worst case. Since \(N \le 2 \times 10^5\), this would require about \(4 \times 10^{10}\) operations and result in TLE.

Focusing on the Properties of the Range

Notice that \(X\) is monotonically increasing. The condition for an index \(i\) to be a valid transition source for \(j\) is:

\[X_i \ge X_j - K \quad \text{and} \quad i < j\]

Since \(X\) is an increasing sequence, the set of indices \(i\) satisfying this condition forms a contiguous range \([L_j,\ j-1]\). Furthermore, as \(j\) increases, \(X_j - K\) also increases, so the left endpoint \(L_j\) is monotonically non-decreasing.

In other words, what we need is to quickly query the maximum value of \(dp\) within a window whose right endpoint extends by 1 each step and whose left endpoint also advances monotonically. This is precisely the structure of the sliding window maximum, which can be handled in overall \(O(N)\) time using a monotonic deque.

(Querying range maximums using a segment tree would also pass in \(O(N \log N)\) time, but a deque is simpler and faster.)

Algorithm

In a deque dq, we maintain the “indices that could potentially be candidates for the maximum in the future” such that their \(dp\) values are monotonically non-increasing. We process \(j = 1, 2, \dots, N-1\) (0-indexed) in order as follows:

  1. Add element: Add the previous index \(p = j-1\) to the window. Before adding it, pop elements from the back of the deque as long as \(dp[\text{back}] \le dp[p]\) (since \(p\) is newer and has a greater or equal value, the elements at the back can never become the maximum). Then, push \(p\) to the back. However, if \(dp[p] = -\infty\) (unreachable), it cannot be a candidate, so do not add it.
  2. Remove out-of-range elements: While the front element satisfies \(X_{\text{front}} < X_j - K\), pop it from the front (it is a foothold that can no longer reach \(j\)).
  3. Determine the value: If the deque is empty, \(dp[j] = -\infty\) (unreachable). Otherwise, the front element holds the maximum value within the window, so \(dp[j] = dp[\text{front}] + A_j\).

Finally, if \(dp[N-1]\) is \(-\infty\), output -1; otherwise, output \(dp[N-1]\).

Concrete Example

Consider \(N=4,\ K=3\), and \((X,A) = (1,5),(2,-10),(4,-10),(5,100)\):

  • \(dp[1] = 5\) (foothold 1)
  • \(j=2\) (\(X=2\)): The window requires \(X_i \ge -1\), so it contains \(\{1\}\). \(dp[2] = 5 + (-10) = -5\).
  • \(j=3\) (\(X=4\)): The window requires \(X_i \ge 1\), so it contains \(\{1,2\}\), with the maximum being \(dp[1]=5\). \(dp[3] = 5 - 10 = -5\).
  • \(j=4\) (\(X=5\)): The window requires \(X_i \ge 2\), so it contains \(\{2,3\}\) (foothold 1 is at distance 4 and cannot reach \(j\)). The maximum is \(-5\). \(dp[4] = -5 + 100 = 95\).

The answer is \(95\). This properly reflects the structure where we must step on at least one foothold with a negative score.

Complexity

  • Time complexity: \(O(N)\) (each index enters and leaves the deque at most once)
  • Space complexity: \(O(N)\)

Implementation Details

  • Representation of Unreachability: Since the absolute value of \(A_i\) is at most \(10^9\) and there are at most \(2\times10^5\) footholds, the minimum possible total score is around \(-2\times10^{14}\). Therefore, the sentinel value representing “unreachable” must be significantly smaller than this (e.g., LLONG_MIN/4). While a value like -1e18 is also fine, it is safest to prevent unreachable indices from being inserted into the deque (i.e., never add to them) to avoid overflow during addition.

  • Handling an Answer of \(-1\): As stated in the problem description, “if foothold \(N\) cannot be reached, output \(-1\)” and “if it is reachable and the maximum score is \(-1\), output \(-1\)”. There is no need to distinguish between these two cases, which simplifies implementation.

  • Overflow: Since the total score can reach around \(2\times10^{14}\), always store \(dp\) using 64-bit integers (long long). It is also safer to use long long for \(K\) and \(X\) as well, since comparisons like \(X_i < X_j - K\) involve operations between numbers up to \(10^9\).

  • Order of Window Maintenance: By following the order “add \(j-1 \to\) remove out-of-range elements \(\to\) get the maximum”, the window will always correctly represent “\(i < j\) and \(X_j - X_i \le K\)”. If the addition and removal order were reversed, a bug could occur where \(j-1\) itself is out of range but remains in the deque (since \(X_j - X_{j-1} \le K\) is not guaranteed, it is important to perform the removal check after adding).

  • Case \(N=2\): If \(X_2 - X_1 > K\), foothold \(2\) is unreachable and the answer is \(-1\). The loop above naturally handles this case as well.

    Source Code

#include <bits/stdc++.h>
using namespace std;

int main(){
    int N; long long K;
    scanf("%d %lld", &N, &K);
    vector<long long> X(N), A(N);
    for(int i=0;i<N;i++) scanf("%lld %lld", &X[i], &A[i]);
    const long long NEG = LLONG_MIN/4;
    vector<long long> dp(N, NEG);
    dp[0]=A[0];
    deque<int> dq;
    for(int j=1;j<N;j++){
        int p=j-1;
        if(dp[p]!=NEG){
            while(!dq.empty() && dp[dq.back()]<=dp[p]) dq.pop_back();
            dq.push_back(p);
        }
        while(!dq.empty() && X[dq.front()] < X[j]-K) dq.pop_front();
        if(dq.empty()) dp[j]=NEG;
        else dp[j]=dp[dq.front()]+A[j];
    }
    if(dp[N-1]==NEG) printf("-1\n");
    else printf("%lld\n", dp[N-1]);
    return 0;
}

This editorial was generated by claude5opus-high.

posted:
last update: