Official

E - スタンプラリー / Stamp Rally Editorial by admin Editorial for printing

Claude Opus 5 (High)

Overview

In this problem, we move along a number line at a speed of at most \(1\) and collect stamps from checkpoints by passing through coordinate \(X_i\) on or before deadline \(T_i\). We solve this using interval DP (with states tracking the left endpoint, right endpoint, the current side, and the number of stamps collected), utilizing the property that “the visited range is always an interval containing \(0\).”

Analysis

Observation 1: Visited Coordinates Form an Interval

Since Takahashi moves continuously, the set of coordinates visited by time \(t\) always forms a closed interval \([L, R]\) containing \(0\) (\(L \le 0 \le R\)). Therefore:

  • Whether the stamp for checkpoint \(i\) can be collected depends solely on whether the time of first arrival at \(X_i\) is \(\le T_i\) (visiting a previously visited point again will not result in an earlier time, so doing so is pointless).
  • Furthermore, a new checkpoint is reached for the first time only when the interval boundaries \(L\) or \(R\) are expanded.

Observation 2: Turning Around Only Needs to Occur at Checkpoints

The sole purpose of moving is to “reach a new checkpoint.” Turning around at a non-checkpoint location only wastes time and is never optimal. Additionally, stopping along the way is never advantageous (since deadlines are upper bounds of the form “on or before”).

Thus, optimal actions can be restricted to repeatedly:

“Head at full speed from the current endpoint toward either the nearest unvisited checkpoint to the left or the nearest unvisited checkpoint to the right.”

The only unvisited checkpoint passed during this movement is the destination itself.

Observation 3: Issues with Naive Approaches

  • Exhaustive search: Since there is a choice of going left or right each time, there are \(2^N\) possibilities, which will not run in time.
  • Greedy (e.g., collecting in order of earliest deadline): For example, when there is a “nearby point with a loose deadline” and a “farther point with a strict deadline,” the outcome depends on which one is visited first, so a simple greedy approach fails.
  • Defining a DP as \(dp[\text{left}][\text{right}][\text{current side}] = \) minimum time is also natural, but this fails to properly track the number of stamps. There may be multiple paths reaching the same interval and the same side:
    • A path with an earlier time but fewer stamps
    • A path with a later time but more stamps

Which of these is ultimately more advantageous depends on subsequent deadlines. Therefore, we should include the number of collected stamps \(k\) in the state and store the “minimum time to finish visiting interval \([i,j]\), end up at side \(s\), and have collected \(k\) stamps” (for the same \(k\), an earlier time is strictly more advantageous, so this is sufficient).

Algorithm

First, divide the checkpoints into the negative side and the positive side:

  • Sort points on the negative side in increasing order of distance from \(0\): distances \(A_1 < A_2 < \dots < A_p\), deadlines \(TA_1,\dots,TA_p\)
  • Sort points on the positive side in increasing order of distance: \(B_1 < B_2 < \dots < B_q\), deadlines \(TB_1,\dots,TB_q\)

Define the DP state as:

\[dp[i][j][s][k] = \text{(minimum time when points up to } -A_i \text{ on the left and } B_j \text{ on the right are visited, currently at side } s \text{, with } k \text{ stamps collected)}\]

(where \(s=0\) means the current position is \(-A_i\), and \(s=1\) means \(B_j\)). The initial state is \(dp[0][0][0][0]=dp[0][0][1][0]=0\) (coordinate \(0\), \(A_0=B_0=0\)).

There are only two transitions. Letting the current position be \(c\) and the current time be \(t\):

  1. Extend to the left (\(i<p\)): Destination is \(-A_{i+1}\), arrival time is \(t' = t + |c - (-A_{i+1})|\), and update \(dp[i+1][j][0][k']\) with \(t'\) where \(k' = k + [\,t' \le TA_{i+1}\,]\).
  2. Extend to the right (\(j<q\)): Destination is \(B_{j+1}\), arrival time is \(t' = t + |c - B_{j+1}|\), and update \(dp[i][j+1][1][k']\) with \(t'\) where \(k' = k + [\,t' \le TB_{j+1}\,]\).

Since \(i\) and \(j\) only increase monotonically, iterating loops in increasing order of \(i, j\) will process states in the correct order.

The answer is the maximum value of \(k\) among all reachable states (states whose value is not \(\mathrm{INF}\)).

Passing through points after their deadlines simply means they are not counted (\(k\) does not increase), but Takahashi is free to pass through them regardless, so this correctly computes the maximum value.

Complexity

The number of states is \(O(N^2 \cdot 2 \cdot N) = O(N^3)\), and each state transition takes \(O(1)\).

  • Time Complexity: \(O(N^3)\) (since \(N \le 100\), around \(2\times10^6\) operations, which is sufficiently fast)
  • Space Complexity: \(O(N^3)\) (a long long array of roughly \(102^3 \cdot 2\), well within a few tens of MB)

Implementation Notes

  • Treating the negative side by converting to absolute values allows writing symmetric code for both sides (when calculating the current position, revert the sign back to \(-A_i\) when \(s=0\)).

  • Since \(X_i \ne 0\) is guaranteed, there is no checkpoint at the starting point, so we can straightforwardly treat \(i=j=0\) as coordinate \(0\).

  • Coordinates are up to \(10^9\) and \(N=100\), so the total travel time can reach up to around \(10^{11}\). Be sure to use 64-bit integers.

  • For INF representing unreachable states, choose a sufficiently large value that will not overflow upon addition (e.g., use \(4\times10^{18}\) and continue before performing addition).

  • The upper bound for \(k\) is \(i+j\) (the number of points visited so far), so restricting the inner loop to \(k \le i+j\) reduces unnecessary computations.

  • Setting “the initial state to \(0\) for both \(s=0, 1\)” naturally handles whether Takahashi moves left or right first.

    Source Code

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

static long long dp[102][102][2][102];

int main(){
    int n;
    if(scanf("%d",&n)!=1) return 0;
    vector<pair<long long,long long>> neg, pos;
    for(int i=0;i<n;i++){
        long long x,t;
        scanf("%lld %lld",&x,&t);
        if(x<0) neg.push_back({-x,t});
        else pos.push_back({x,t});
    }
    sort(neg.begin(),neg.end());
    sort(pos.begin(),pos.end());
    int p=neg.size(), q=pos.size();
    vector<long long> A(p+1,0), TA(p+1,0), B(q+1,0), TB(q+1,0);
    for(int i=1;i<=p;i++){A[i]=neg[i-1].first; TA[i]=neg[i-1].second;}
    for(int j=1;j<=q;j++){B[j]=pos[j-1].first; TB[j]=pos[j-1].second;}

    const long long INF = (long long)4e18;
    for(int i=0;i<=p;i++)for(int j=0;j<=q;j++)for(int s=0;s<2;s++)for(int k=0;k<=p+q;k++) dp[i][j][s][k]=INF;
    dp[0][0][0][0]=0;
    dp[0][0][1][0]=0;

    int ans=0;
    for(int i=0;i<=p;i++){
        for(int j=0;j<=q;j++){
            for(int s=0;s<2;s++){
                for(int k=0;k<=i+j;k++){
                    long long t=dp[i][j][s][k];
                    if(t>=INF) continue;
                    ans=max(ans,k);
                    long long pos_cur = (s==1 ? B[j] : -A[i]);
                    // move to next negative
                    if(i<p){
                        long long target = -A[i+1];
                        long long nt = t + llabs(pos_cur - target);
                        int nk = k + (nt <= TA[i+1] ? 1 : 0);
                        if(nt < dp[i+1][j][0][nk]) dp[i+1][j][0][nk]=nt;
                    }
                    if(j<q){
                        long long target = B[j+1];
                        long long nt = t + llabs(pos_cur - target);
                        int nk = k + (nt <= TB[j+1] ? 1 : 0);
                        if(nt < dp[i][j+1][1][nk]) dp[i][j+1][1][nk]=nt;
                    }
                }
            }
        }
    }
    printf("%d\n",ans);
    return 0;
}

This editorial was generated by claude5opus-high.

posted:
last update: