J - コマの移動 / Moving the Piece Editorial by admin Editorial for printing
Claude Opus 5 (High)Overview
This problem asks us to simulate \(N\) operations where obstacles are placed on a number line one by one, and whenever an obstacle is placed directly beneath the piece (at the same coordinate), the piece jumps to the “nearest unoccupied coordinate”. We can process this efficiently by managing the set of obstacles as a “set of contiguous intervals”.
Analysis
Observation 1: The piece’s coordinate is always unoccupied
Initially, there are no obstacles, so coordinate \(0\) is unoccupied. After that, whenever an obstacle is placed at the coordinate where the piece is located, the piece always moves to an “unoccupied coordinate”. Thus, the invariant that the coordinate where the piece is currently located never has an obstacle at any point in time is maintained.
Therefore, if “an obstacle is placed again at a coordinate \(X_i\) that already contains an obstacle”, the piece cannot be there, so no movement occurs.
Observation 2: The destination can only be just outside the ends of the “contiguous block of obstacles”
Suppose at event \(i\), the piece’s position is equal to \(X_i\), and an obstacle is placed at \(X_i\). Consider the maximal contiguous obstacle interval \([l, r]\) containing \(X_i\) (that is, an interval where there are obstacles at all \(y\) such that \(l \le y \le r\), and no obstacles at \(l-1\) and \(r+1\)). Since every coordinate from \(l\) to \(r\) is occupied, the only two candidates for the nearest unoccupied coordinates are:
- Left: \(l-1\) (distance: \(X_i - (l-1)\))
- Right: \(r+1\) (distance: \((r+1) - X_i\))
We only need to compare these two distances and move to the closer one; if the distances are equal, according to the problem statement, we move to the right, which is \(r+1\).
Example: Suppose the obstacles become \(\{-1,0,1,2\}\) after an obstacle is placed at \(X_i=1\) where the piece is currently located (since it wouldn’t move if an obstacle was already there, assume \(1\) was newly added). The block containing \(1\) is \([-1,2]\), so the candidates are \(-2\) (distance \(3\)) and \(3\) (distance \(2\)), and the piece moves to \(3\).
Why a Naive Approach Fails
- Since the coordinates satisfy \(-10^9 \le X_i \le 10^9\), a boolean array directly indexed by coordinates (of length \(2\times10^9\)) cannot be used.
- If we store obstacles in a
setand search for unoccupied coordinates cell by cell to the left and right starting from \(X_i\), when blocks become long, each event could require \(O(N)\) steps. In the worst case, this leads to an \(O(N^2)\) time complexity (around \(10^{10}\) operations), which will result in TLE (e.g., placing obstacles sequentially at \(0,1,2,\dots\) forces walking through a long block every time).
Therefore, by maintaining the obstacles as a “set of merged intervals”, we can obtain the endpoints \(l, r\) of the block in \(O(\log N)\) time.
Algorithm
We maintain a map<long long, long long> mp storing “left endpoint \(\to\) right endpoint” of intervals such that they are non-overlapping and non-adjacent (fully merged). For each event with \(x = X_i\):
- Check if an interval contains \(x\)
Find
it = mp.upper_bound(x)(the first interval with a left endpoint strictly greater than \(x\)), and examine its predecessorpit.- If
pit->second >= x, then \(x\) is already contained in the interval \([\,\)pit->first\(,\,\)pit->second\(\,]\) (= an obstacle already exists). In this case, the set of intervals does not change.
- If
- If not contained, insert and merge the interval
Let the new interval be \([l, r] = [x, x]\), and:
- If the right endpoint of the interval to the left is \(x-1\), merge with it (\(l \leftarrow\) left endpoint of the left interval, and erase the left interval).
- If the left endpoint of the interval to the right is \(x+1\), merge with it (\(r \leftarrow\) right endpoint of the right interval, and erase the right interval).
- Register
mp[l] = r.
- Movement of the piece
Only when the piece’s position is equal to \(x\), use the interval \([l, r]\) containing \(x\) obtained above to compute:
- \(d_L = x - (l-1)\), \(d_R = (r+1) - x\)
- If \(d_R \le d_L\), then \(pos \leftarrow r+1\); otherwise, \(pos \leftarrow l-1\).
Finally, output \(pos\).
By Observation 1, if it was “already contained” in step 1, the piece’s position is guaranteed to be \(\ne x\), so step 3 is automatically skipped (in the code, as long as the interval \([l, r]\) is correctly identified, checking unconditionally is also completely fine).
Complexity
- Time Complexity: \(O(N \log N)\) (a constant number of
maplookups, insertions, and deletions per event) - Space Complexity: \(O(N)\) (the number of intervals is at most the number of obstacles)
Implementation Notes
Coordinate compression is also possible, but managing intervals with a
mapallows for straightforward, online processing, which is simpler.When the result of
upper_bound(x)isbegin(), there is no left neighbor, so be careful not to callprev. Similarly, when it isend(), there is no right neighbor.When \(x\) is already contained, do not erase or re-insert the interval (leave it as is). Also, extracting \(l, r\) even when it was already contained can simplify the code by reducing branches for the movement check.
When the distances are equal (\(d_L = d_R\)), the piece moves to the right, so the comparison should include the equality on the right side, like
if (dr <= dl) pos = r+1; else pos = l-1;.Since \(|X_i| \le 10^9\), \(l-1\) and \(r+1\) also stay around this range, so
intis technically sufficient, but usinglong longis safer.Since there are up to \(N \le 10^5\) lines of input/output, it is recommended to use
scanf/printfor fast I/O withcin(ios::sync_with_stdio(false)).Source Code
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
if(scanf("%d",&n)!=1) return 0;
map<long long,long long> mp; // l -> r merged intervals of obstacles
long long pos = 0;
for(int i=0;i<n;i++){
long long x;
scanf("%lld",&x);
bool contained=false;
long long l=x,r=x;
auto it=mp.upper_bound(x);
if(it!=mp.begin()){
auto pit=prev(it);
if(pit->second>=x){
contained=true;
l=pit->first; r=pit->second;
} else if(pit->second==x-1){
l=pit->first;
mp.erase(pit);
}
}
if(!contained){
if(it!=mp.end() && it->first==x+1){
r=it->second;
mp.erase(it);
}
mp[l]=r;
}
if(pos==x){
long long dl = x - (l-1);
long long dr = (r+1) - x;
if(dr<=dl) pos = r+1;
else pos = l-1;
}
}
printf("%lld\n",pos);
return 0;
}
This editorial was generated by claude5opus-high.
posted:
last update: