#P10577. [蓝桥杯 2024 国 A] 兔子集结

[蓝桥杯 2024 国 A] 兔子集结

Problem Description

In a quiet corner of the forest, there is a village where nn rabbits live.

On a moonlit night, these rabbits line up in a row and prepare to start a rally jumping activity. Each rabbit occupies a position in the village. For rabbit ii, its position is pip_i. We call a smaller position “to the left” and a larger position “to the right”.

According to the village tradition, each rabbit will choose the rabbit closest to itself as its partner and jump in the direction of that partner. If the nearest rabbit on the left and on the right are at the same distance, it will choose the rabbit on the left as its partner.

Each jump of a rabbit can only move one unit to the left or to the right. That is, if a rabbit is currently at position xx, then after its next jump it will be at x1x-1 or x+1x+1.

When the distance between two rabbits moving toward each other becomes 11, the rabbit on the left will stay where it is, while the rabbit on the right will jump onto the left rabbit’s position, completing the rally.

The rabbits will keep jumping until they complete the rally with the partner they initially chose, and then stop.

After all rabbits have completed the rally, at which position will each rabbit end up?

Input Format

The first line contains an integer nn, representing the number of rabbits.
The second line contains nn integers p1,p2,,pnp_1,p_2,\cdots,p_n, separated by single spaces, representing the initial positions of the rabbits.

Output Format

Output one line containing nn integers, representing the position of each rabbit after it completes the rally.

5
2 5 7 9 1
1 6 6 6 1

Hint

Sample Explanation

Rabbit 11 chooses rabbit 55 as its partner, and rabbit 55 chooses rabbit 11 as its partner. Since the distance between them is 11, rabbit 55 stays where it is, while rabbit 11 jumps to position 11 and completes the rally with rabbit 55.

Rabbit 22 chooses rabbit 33 as its partner, and rabbit 44 also chooses rabbit 33 as its partner. For rabbit 33, rabbits 22 and 44 are at the same distance from it, so it chooses the left rabbit 22 as its partner. Rabbit 22 jumps one position to the right, rabbit 33 jumps one position to the left, and rabbit 44 jumps one position to the left. At this moment, rabbits 22 and 33 are both at position 66, completing the rally (stopping jumping). Rabbit 44 will continue jumping left until it jumps to position 66 and completes the rally with rabbit 33.

Constraints

For 40%40\% of the testdata, 2n5002\le n\le 500, 1pi1051\le p_i\le 10^5, and p1,p2,,pnp_1,p_2,\cdots,p_n are all distinct.
For all testdata, 2n1052\le n \le 10^5, 1pi1091 \le p_i \le 10^9, and p1,p2,,pnp_1,p_2,\cdots,p_n are all distinct.

Translated by ChatGPT 5