Official

J - コマの移動 / Moving the Piece Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

We add obstacles one by one, and only when an obstacle is placed at the piece’s position, we move it to the nearest empty coordinate. By managing the lengths of contiguous intervals of obstacles using a dictionary, we can quickly find the destination.

Analysis

Destination Is Just Outside the Contiguous Interval

Suppose an obstacle is added at coordinate \(x\) where the piece is located. If the contiguous interval of obstacles containing \(x\) after the addition is \([L,R]\), there are only \(2\) candidate destinations:

  • The nearest empty coordinate to the left: \(L-1\)
  • The nearest empty coordinate to the right: \(R+1\)

This is because the entire interval contains obstacles, and moving further outside these \(2\) coordinates would only increase the distance.

For example, if obstacles continuously exist in \([2,6]\) and the piece is at \(4\), the candidates are \(1\) and \(7\). Since both have a distance of \(3\), we choose \(7\) on the right.

Searching for an Empty Coordinate Every Time Is Too Slow

If we check coordinates one by one to the left and right each time the piece moves, we might traverse long intervals of obstacles repeatedly. This would require up to \(O(N)\) operations per event, potentially leading to an overall time complexity of \(O(N^2)\).

Therefore, we need a way to find the endpoints of the contiguous interval directly.

Recording the Length at Both Ends of an Interval

We use a dictionary lengths to maintain the following information:

  • Every coordinate that contains an obstacle exists as a key in the dictionary.
  • For each contiguous interval, we record the length of that interval at its left and right endpoints.

The values stored at coordinates strictly inside an interval can remain outdated.

Let \(x\) be the coordinate where a new obstacle is placed. Since \(x\) was previously empty:

  • If there is an obstacle at \(x-1\), it is the right endpoint of the adjacent interval to the left.
  • If there is an obstacle at \(x+1\), it is the left endpoint of the adjacent interval to the right.

Thus, we can correctly retrieve the lengths of the adjacent intervals from the values in the dictionary at both neighboring coordinates.

Algorithm

Let the coordinate of the piece be position, initialized to \(0\). Initialize the dictionary lengths as empty.

For each event at coordinate \(x\), perform the following steps:

1. Do Nothing If an Obstacle Already Exists

If x in lengths, skip this event.

At the end of each event, the piece is always at a coordinate without an obstacle. Therefore, placing an obstacle at a coordinate that already contains one will never cause the piece to move.

2. Get the Lengths of the Left and Right Intervals

Let the lengths of the contiguous intervals to the left and right of \(x\) be left and right, respectively. If there is no obstacle adjacent to it, the length is \(0\).

left = lengths.get(x - 1, 0)
right = lengths.get(x + 1, 0)

Adding an obstacle at \(x\) connects the left and right intervals with \(x\), forming a new interval:

\([x-\text{left},\ x+\text{right}]\)

Its length is:

\(\text{size}=\text{left}+\text{right}+1\)

3. Record the Information for the New Interval

lengths[x] = size
lengths[x - left] = size
lengths[x + right] = size

Setting lengths[x] records that an obstacle exists at \(x\). We also record the correct length at both ends of the new interval.

For example, if obstacles exist in \([2,3]\) and \([5,7]\), and we add \(x=4\), left is \(2\) and right is \(3\). The new interval becomes \([2,7]\), and we record a length of \(6\) at both endpoints \(2\) and \(7\).

4. Move the Piece If Necessary

If position == x, the candidate destinations and their distances are as follows:

Direction Destination Distance
Left \(x-\text{left}-1\) \(\text{left}+1\)
Right \(x+\text{right}+1\) \(\text{right}+1\)

Therefore:

  • If left < right, move left.
  • Otherwise, move right.

This also satisfies the tie-breaking condition of choosing the right side when distances are equal.

After processing all events, output position.

Why This Approach Works

Adding a new obstacle only affects that coordinate and its immediately adjacent intervals to the left and right. By merging these and updating the lengths at the new endpoints, we maintain the invariant that “the correct length is stored at both endpoints of each interval.”

Moreover, when the piece moves, we compare the \(2\) coordinates immediately outside the interval containing the piece, ensuring that the destination is chosen according to the problem’s rules.

Complexity

  • Time Complexity: \(O(N)\) (assuming hash map lookups and updates take \(O(1)\) on average)
  • Space Complexity: \(O(N)\)

A constant number of dictionary operations are performed per event. Keys are added to the dictionary only for distinct coordinates where obstacles are placed.

Implementation Notes

  • There is no need to update values inside an interval. When a new coordinate is added, its adjacent coordinates (if they contain obstacles) are guaranteed to be the endpoints of existing intervals, so the referenced values are always valid.

  • Move to the right in case of a tie. The condition to choose left is left < right, not left <= right.

  • Do not use an array directly indexed by coordinates. Since the range of coordinates is large and can include negative values, use a dictionary to manage only the necessary coordinates.

    Source Code

import sys


def main():
    data = list(map(int, sys.stdin.buffer.read().split()))
    lengths = {}
    position = 0

    for x in data[1:]:
        if x in lengths:
            continue

        left = lengths.get(x - 1, 0)
        right = lengths.get(x + 1, 0)
        size = left + right + 1

        lengths[x] = size
        lengths[x - left] = size
        lengths[x + right] = size

        if position == x:
            if left < right:
                position = x - left - 1
            else:
                position = x + right + 1

    print(position)


if __name__ == "__main__":
    main()

This editorial was generated by gpt-6-astra-medium.

posted:
last update: