E - スタンプラリー / Stamp Rally 解説 by admin
gpt-6-astra-mediumOverview
We use interval DP by utilizing the fact that the visited checkpoints always form a contiguous interval on the number line. We maintain the earliest arrival time for each combination of the visited interval, current position, and the number of collected stamps.
Analysis
1. Visited checkpoints form a contiguous interval
Sort the checkpoints by coordinate, and add the starting point at coordinate \(0\) as a dummy checkpoint.
On a number line, whenever you move to a certain point, you must pass through all the points along the way. Therefore, the set of checkpoints visited at least once always forms a contiguous interval containing the starting point when ordered by coordinate.
For example, suppose there are checkpoints at coordinates \(-5,-2,3\). If you head toward \(-5\) from coordinate \(0\), you will inevitably visit \(-2\) on the way. Thus, it is impossible to have visited only \(-5\) while leaving \(-2\) unvisited.
Letting the visited interval be \([l,r]\), the next checkpoint to be visited for the first time must be one of the following:
- The left neighbor, \(l-1\)
- The right neighbor, \(r+1\)
Note that “visited” here includes points where the deadline had already passed and no stamp could be collected.
2. Consider only the moments immediately after reaching a new point
Immediately after visiting a new checkpoint for the first time, Takahashi is located at either the left end or the right end of the visited interval.
Furthermore, stamps only have deadlines, so arriving early is never a disadvantage. Once the next point to visit for the first time is decided, it is optimal to move there in the shortest possible time without waiting or making unnecessary round trips along the way.
For any already-visited point:
- If visited within the deadline, the stamp was already collected on the first visit.
- If the deadline had passed, returning to it later will still not allow collecting the stamp.
Therefore, no new stamps can be collected inside the already-visited interval.
3. Consider both arrival time and the number of collected stamps
If we were to enumerate all choices of expanding the interval to the left or right, the choices would repeatedly branch, leading to exponential time complexity.
We therefore use DP, but keeping only the visited interval and the current position as the state is insufficient.
For example, suppose we reach the same interval and the same endpoint through two different states:
- Arriving at time \(10\) with \(2\) stamps
- Arriving at time \(12\) with \(3\) stamps
Which state is more advantageous depends on future deadlines. We cannot discard one over the other simply by looking at the arrival time alone or the number of stamps alone.
On the other hand, if the visited interval, current position, and the number of collected stamps are all identical, the state with the earlier arrival time is strictly better. Leaving earlier ensures reaching subsequent checkpoints no later, so the number of stamps that can be collected in the future will never decrease.
Utilizing this property, we include the number of collected stamps in the DP state.
Algorithm
1. Preprocessing
Add a dummy point at coordinate \(0\) and sort all points in ascending order of coordinate.
Let \(M=N+1\) be the number of points, and let \(o\) be the index of the starting point. Hereafter, let \(X_i\) and \(T_i\) denote the sorted coordinates and deadlines, respectively.
No stamp is collected at the dummy point.
2. DP State
Let \(dp[l][r][k][s]\) be the minimum time to reach the following state:
- The visited points form the interval \([l,r]\)
- The number of collected stamps is \(k\)
- If \(s=0\), the current position is the left end \(X_l\)
- If \(s=1\), the current position is the right end \(X_r\)
Unreachable states are set to \(\mathrm{INF}\).
The initial state corresponds to having visited only the starting point at time \(0\), so:
\(dp[o][o][0][0]=dp[o][o][0][1]=0\)
3. Transitions
Let \(t\) be the current arrival time and \(p\) be the current coordinate. The current coordinate is \(X_l\) if \(s=0\), and \(X_r\) if \(s=1\).
Moving to the left neighbor
If \(l>0\), we can move to point \(l-1\).
The arrival time is:
\(a=t+p-X_{l-1}\)
If we arrive within the deadline, we collect \(1\) stamp. Thus, setting the next number of stamps to:
\(k'=k+\mathbf{1}(a\le T_{l-1})\)
we update:
\(dp[l-1][r][k'][0]\leftarrow \min(dp[l-1][r][k'][0],a)\)
Here, \(\mathbf{1}(\text{condition})\) is \(1\) if the condition is true and \(0\) if false.
Moving to the right neighbor
If \(r+1<M\), we can move to point \(r+1\).
The arrival time is:
\(a=t+X_{r+1}-p\)
Similarly, setting:
\(k'=k+\mathbf{1}(a\le T_{r+1})\)
we update:
\(dp[l][r+1][k'][1]\leftarrow \min(dp[l][r+1][k'][1],a)\)
Even if we do not make it in time for the deadline, the transition for the move is still performed. This is because there might still be checkpoints further ahead where stamps can be collected.
4. Computation Order and the Answer
In both transitions, the length of the visited interval increases by \(1\). Therefore, we compute the DP in increasing order of interval length.
It is sufficient to consider only intervals that contain the starting point \(o\).
The answer is the maximum value of \(k\) among all reachable states.
Why this approach is correct
Since the first visit to any new point always occurs at either the immediate left or immediate right of the visited interval, the two types of transitions above cover all possible visitation orders.
Furthermore, performing each move in the minimum time never decreases the number of stamps collected. Among the paths that reach the same interval, current position, and number of stamps, discarding slower ones is safe because continuing from an earlier arrival state allows collecting at least as many stamps.
Therefore, keeping only the minimum time for each state does not miss the optimal solution and correctly finds the answer.
Complexity
There are \(O(N^2)\) intervals \([l,r]\), \(O(N)\) possibilities for the number of stamps \(k\), and \(2\) possibilities for the current position. Since there are at most \(2\) transitions from each state, the complexity is as follows:
- Time complexity: \(O(N^3)\)
- Space complexity: \(O(N^3)\)
Implementation Notes
Arriving exactly at the deadline is valid. Use
arrival <= T[i]for the check.Use
long longfor timestamps. With repeated back-and-forth moves, the accumulated travel time can exceed the range of a 32-bit integer.Do not count a stamp at the starting point. The initial number of stamps is \(0\). Since the starting point is visited from the beginning, it will never be counted anew during transitions.
An interval of length \(\mathrm{len}\) containing the starting point contains \(\mathrm{len}-1\) real checkpoints. Therefore, it is sufficient to check \(k\) only in the range \(0\le k<\mathrm{len}\).
In the code, the DP array is flattened into one dimension and accessed via
state(l, r, k)[side]. This is merely a different way of laying out memory; the underlying concept is identical to a 4-dimensional interval DP.Source Code
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<pair<long long, long long>> checkpoints(N);
for (auto &[x, t] : checkpoints) cin >> x >> t;
checkpoints.emplace_back(0, 0);
sort(checkpoints.begin(), checkpoints.end());
const int M = N + 1;
int origin = 0;
vector<long long> X(M), T(M);
for (int i = 0; i < M; ++i) {
X[i] = checkpoints[i].first;
T[i] = checkpoints[i].second;
if (X[i] == 0) origin = i;
}
const long long INF = (1LL << 60);
vector<array<long long, 2>> dp(
static_cast<size_t>(M) * M * M, array<long long, 2>{INF, INF}
);
auto state = [&](int l, int r, int k) -> array<long long, 2>& {
return dp[(static_cast<size_t>(l) * M + r) * M + k];
};
state(origin, origin, 0) = {0, 0};
int answer = 0;
for (int len = 1; len <= M; ++len) {
int first = max(0, origin - len + 1);
int last = min(origin, M - len);
for (int l = first; l <= last; ++l) {
int r = l + len - 1;
for (int k = 0; k < len; ++k) {
const auto current = state(l, r, k);
for (int side = 0; side < 2; ++side) {
long long time = current[side];
if (time == INF) continue;
answer = max(answer, k);
long long position = X[side == 0 ? l : r];
if (l > 0) {
long long arrival = time + position - X[l - 1];
int nextK = k + (arrival <= T[l - 1]);
auto &next = state(l - 1, r, nextK)[0];
next = min(next, arrival);
}
if (r + 1 < M) {
long long arrival = time + X[r + 1] - position;
int nextK = k + (arrival <= T[r + 1]);
auto &next = state(l, r + 1, nextK)[1];
next = min(next, arrival);
}
}
}
}
}
cout << answer << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.