E - スタンプラリー / Stamp Rally 解説 by admin
Claude Opus 5 (High)Overview
This problem asks for the maximum number of stamps that can be collected by moving along the number line with a speed of at most \(1\) and passing each checkpoint \(i\) at or before time \(T_i\). We solve it using DP, where the state tracks how far the left and right endpoints of the visited interval have been extended.
Analysis
Observation 1: The visited range is always an interval containing \(0\)
Since Takahashi starts at coordinate \(0\) at time \(0\) and moves continuously, at any point in time, the set of already-visited coordinates always forms an interval \([-a,\;b]\) (\(a,b \ge 0\)) containing \(0\). Furthermore, Takahashi is always at one of the endpoints of this interval (either \(-a\) or \(b\)). (Otherwise, since the endpoint has not been updated yet, time would have been wasted compared to when he was at that endpoint.)
In other words, the movement can be formalized as:
Repeatedly “extend to the left” or “extend to the right” in an arbitrary order, not necessarily alternating.
Observation 2: Turning around only needs to occur at checkpoints
Turning around at a coordinate without a checkpoint is suboptimal (turning around earlier at the checkpoint just visited visits the same set of checkpoints in less time). Stopping midway is also suboptimal. Therefore,
The movement can be viewed as repeatedly: “head directly from the current endpoint to the nearest unvisited checkpoint on the left” or “to the nearest unvisited checkpoint on the right”.
Observation 3: We only need to consider the first time each checkpoint is reached
If we sort the checkpoints on the left side in ascending order of \(|X|\), when we extend the left endpoint to reach the \(l\)-th checkpoint, checkpoints \(1,\dots,l-1\) have already been passed. Therefore, each checkpoint is reached for the first time at the exact moment the endpoint is extended to that point, which is also the earliest possible time it is reached. Since an earlier arrival time makes it easier to satisfy the stamp collection condition \(\text{time} \le T_i\), it suffices to compare this first arrival time with \(T_i\).
Issue with the Naive Approach
Exhaustively searching over all possible orders of extensions would require exploring up to \(2^N\) sequences of extensions, which will not run in time.
On the other hand, maintaining only “\(dp[l][r][s] = \text{minimum time}\)” results in WA. This is because a path that takes less time does not necessarily collect more stamps (e.g., spending time early to collect a distant point with a generous deadline). Thus, by adding “the number of stamps collected so far, \(k\)” to the state and tracking the minimum time for each \((l,r,s,k)\), future reachability from the same state is monotonic with respect to time (an earlier time is always strictly better), making the DP work correctly.
Algorithm
Divide the checkpoints into negative and positive sides, and sort each side in ascending order of \(|X|\): - Negative side: \(lx_0 < lx_1 < \dots\) (absolute values), deadlines \(lt_j\) - Positive side: \(rx_0 < rx_1 < \dots\), deadlines \(rt_j\)
Define the DP state as: $\(dp[l][r][s][k] = (\text{minimum time when the first } l \text{ checkpoints on the negative side and the first } r \text{ checkpoints on the positive side have been visited, currently at the } s\text{-side endpoint, with } k \text{ stamps collected})\)\( (\)s=0\(: at the left endpoint \)-lx{l-1}\(; \)s=1\(: at the right endpoint \)rx{r-1}\(. When \)l=0\( / \)r=0\(, the position is coordinate \)0$).
The initial state is \(dp[0][0][0][0] = dp[0][0][1][0] = 0\) (since we haven’t moved yet, the position is \(0\) regardless of which side is considered).
From the current position \(pos\), the transitions are: - Extend to the left: new time \(nt = t + |pos - (-lx_l)|\); if \(lt_l \ge nt\), \(k+1\); transition to state \((l+1, r, 0)\) - Extend to the right: new time \(nt = t + |rx_r - pos|\); if \(rt_r \ge nt\), \(k+1\); transition to state \((l, r+1, 1)\)
The answer is the maximum \(k\) among all reachable states (states where the time is not \(\infty\)). Since movement can be stopped at any time, intermediate states are also candidates for the answer.
Since \(l\) and \(r\) increase monotonically with each transition, this can be computed with a straightforward forward DP by looping over \(l\) and \(r\) in ascending order.
Complexity
The number of states is \(O(N^2)\) for \(l, r\), \(2\) choices for \(s\), and \(O(N)\) choices for \(k\), totaling \(O(N^3)\). With \(2\) transitions from each state:
- Time complexity: \(O(N^3)\) (since \(N \le 100\), roughly \(2\times10^6\) transitions)
- Space complexity: \(O(N^3)\)
Implementation Notes
By storing coordinates on the negative side by their absolute values \(-X_i\) and sorting both the positive and negative sides, the “next destination to extend to” can be immediately retrieved as the \(l\)-th or \(r\)-th element of the respective array.
When computing the current position, add branching to set the position to \(0\) when \(l=0\) (or \(r=0\)) to prevent out-of-bounds array access.
For the initial state, it is easiest to initialize both \(s=0\) and \(s=1\) to \(0\) (both signify coordinate \(0\)).
The stamp collection condition is “arrival time \(nt \le T\)”; it is sufficient to pass through at or before \(T\), so include the equality sign.
Even if the deadline is missed, passing through the checkpoint is still allowed in order to collect subsequent checkpoints, so make sure to always allow transitions that do not increment \(k\) (ensuring the option to pass through checkpoints that missed their deadlines and proceed further is not lost).
In Python, accessing 4-dimensional lists has significant overhead, so it can be sped up by extracting the row referenced in the inner loop (such as
dp[l+1][r][0]) into a variable beforehand.Source Code
import sys
def main():
data = sys.stdin.buffer.read().split()
n = int(data[0])
Lp = []
Rp = []
for i in range(n):
x = int(data[1 + 2 * i]); t = int(data[2 + 2 * i])
if x < 0:
Lp.append((-x, t))
else:
Rp.append((x, t))
Lp.sort()
Rp.sort()
nl = len(Lp); nr = len(Rp)
lx = [a for a, b in Lp]; lt = [b for a, b in Lp]
rx = [a for a, b in Rp]; rt = [b for a, b in Rp]
K = n
INF = 1 << 60
# dp[l][r][s][k] = min time
dp = [[[[INF] * (K + 1) for _ in range(2)] for _ in range(nr + 1)] for _ in range(nl + 1)]
dp[0][0][0][0] = 0
dp[0][0][1][0] = 0
best = 0
for l in range(nl + 1):
dpl = dp[l]
for r in range(nr + 1):
dplr = dpl[r]
kmax = l + r
if kmax > K:
kmax = K
for s in range(2):
cur = dplr[s]
if s == 0:
pos = -lx[l - 1] if l > 0 else 0
else:
pos = rx[r - 1] if r > 0 else 0
# targets
if l < nl:
a = -lx[l]
da = pos - a
if da < 0:
da = -da
ta = lt[l]
nxtL = dp[l + 1][r][0]
else:
nxtL = None
if r < nr:
b = rx[r]
db = b - pos
if db < 0:
db = -db
tb = rt[r]
nxtR = dpl[r + 1][1]
else:
nxtR = None
for k in range(kmax + 1):
t = cur[k]
if t >= INF:
continue
if k > best:
best = k
if nxtL is not None:
nt = t + da
nk = k + 1 if ta >= nt else k
if nt < nxtL[nk]:
nxtL[nk] = nt
if nxtR is not None:
nt = t + db
nk = k + 1 if tb >= nt else k
if nt < nxtR[nk]:
nxtR[nk] = nt
sys.stdout.write(str(best) + "\n")
main()
This editorial was generated by claude5opus-high.