J - コマの移動 / Moving the Piece Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
We place obstacles one by one and move the piece to the nearest empty coordinate only when an obstacle is placed at its current position. By managing contiguous obstacles as intervals, we can efficiently find the destination.
Analysis
The Destination is Immediately Outside the Contiguous Obstacles
Suppose that after placing an obstacle at the piece’s current position \(x\), the contiguous interval of obstacles containing \(x\) becomes \([L,R]\). Assume this interval cannot be extended further to the left or right.
Since every integer coordinate within this interval contains an obstacle, there are only two candidate destinations:
- Nearest empty coordinate to the left: \(L-1\)
- Nearest empty coordinate to the right: \(R+1\)
Therefore, by comparing the distances \(x-(L-1)\) and \((R+1)-x\), we can determine the destination. If the distances are equal, we choose the right side.
For example, if there are obstacles at all integer coordinates in \([-2,3]\) and the piece is at \(0\), the candidates are \(-3\) and \(4\). Since the distances are \(3\) and \(4\) respectively, the piece moves to \(-3\).
Inspecting Obstacles One by One is Too Slow
If we find the nearest empty coordinates on the left and right by checking square by square, we would repeatedly traverse long sequences of obstacles.
For example, if obstacles are placed in the order \(X_i=i-1\), that is, \(0,1,2,\dots\), the piece keeps moving to the right. In this case, scanning all the way to the empty coordinate on the left each time would require checking \(1,2,3,\dots\) squares, taking \(O(N^2)\) time in total.
Thus, instead of managing obstacles individually, we manage them as intervals of contiguous obstacles. Once the endpoints of an interval are known, the empty coordinates immediately outside can be found directly.
Algorithm
Using C++ map, we store each interval \([L,R]\) in the following format:
- Key: Left endpoint \(L\)
- Value: Right endpoint \(R\)
We maintain the invariant that intervals neither overlap nor touch. For example, \([1,3]\) and \([4,6]\) are merged into \([1,6]\) rather than kept separately.
Let the coordinate of the piece be position, initialized to \(0\). For each event at coordinate \(x\), we perform the following:
1. Inspect Intervals Before and After \(x\)
Using intervals.upper_bound(x), find the first interval whose left endpoint is strictly greater than \(x\). Let this be the right interval.
If the interval immediately preceding it exists, it is either an interval that potentially contains \(x\) or the closest interval to the left.
If the right endpoint of this preceding interval is greater than or equal to \(x\), an obstacle is already present at \(x\). In this case, do nothing and proceed to the next event.
Since the piece is always at an obstacle-free coordinate at the end of each event, the piece can never be at an \(x\) that already contains an obstacle.
2. Add the New Obstacle to the Intervals
First, initialize the new interval as \([L,R]=[x,x]\).
- If the right endpoint of the left interval is \(x-1\), merge with it:
- Update \(L\) to that interval’s left endpoint and remove the old interval.
- If the left endpoint of the right interval is \(x+1\), merge with it:
- Update \(R\) to that interval’s right endpoint and remove the old interval.
Finally, insert the interval \([L,R]\) into the map.
For example, if intervals \([1,3]\) and \([5,7]\) exist and an obstacle is placed at \(x=4\), it merges with both to become \([1,7]\).
Since intervals are originally non-adjacent, checking for merges with at most one interval on the left and one on the right is sufficient.
3. Move the Piece if Necessary
If position != x, the piece does not move.
If position == x, using the newly added and merged interval \([L,R]\), move the piece as follows:
- If \(x-(L-1)<(R+1)-x\), set
positionto \(L-1\). - Otherwise, set
positionto \(R+1\).
This ensures that the right side is chosen when the distances are equal.
Repeat this for all events and output the final position.
Correctness
By merging only adjacent intervals when adding an obstacle, the map always represents the integer coordinates containing obstacles as maximal contiguous intervals without excess or deficiency.
When the piece moves, there are no empty coordinates within the interval containing its position, and the coordinates immediately outside its endpoints are guaranteed to be empty. Comparing the distances to these two points and breaking ties by picking the right side correctly simulates the movement described in the problem.
Complexity
- Time complexity: \(O(N\log N)\)
- Space complexity: \(O(N)\)
There are at most \(N\) intervals. Each event performs a constant number of searches, deletions, and insertions on the map, taking \(O(\log N)\) time per event.
Implementation Notes
Add the obstacle and merge intervals first, then determine the destination.
Since the right side is chosen in case of a tie, the condition to choose the left side should be strictly
<, not<=.Before using
prev(right), check thatright != intervals.begin(). Also, before accessing the right interval, check thatright != intervals.end().In
map, deleting an element does not invalidate iterators pointing to other elements. However, the iterator pointing to the deleted element itself cannot be used afterwards.It is safe to use
long longfor coordinates and distance calculations, as in the code.Source Code
#include <iostream>
#include <map>
#include <iterator>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
map<long long, long long> intervals;
long long position = 0;
for (int i = 0; i < N; ++i) {
long long x;
cin >> x;
auto right = intervals.upper_bound(x);
long long L = x, R = x;
if (right != intervals.begin()) {
auto left = prev(right);
if (left->second >= x) {
continue;
}
if (left->second == x - 1) {
L = left->first;
intervals.erase(left);
}
}
if (right != intervals.end() && right->first == x + 1) {
R = right->second;
intervals.erase(right);
}
intervals.emplace(L, R);
if (position == x) {
long long left_destination = L - 1;
long long right_destination = R + 1;
if (x - left_destination < right_destination - x) {
position = left_destination;
} else {
position = right_destination;
}
}
}
cout << position << '\n';
return 0;
}
This editorial was generated by gpt-6-astra-medium.
posted:
last update: