#P3165. Sorting Arm Operation Sequence
Sorting Arm Operation Sequence
Sorting Arm Operation Sequence
To sort items of varying heights in a factory from low to high, engineers invented a sorting robotic arm. The arm follows a simple sorting rule: in the first operation, it finds the position \(P_1\) of the smallest item and reverses the items from the first position through \(P_1\) (i.e. the segment \([1,P_1]\)). In the second operation, it finds the position \(P_2\) of the second smallest item and reverses the items from the second position through \(P_2\) (i.e. the segment \([2,P_2]\)), and so on until all items are sorted.
Note: If two items have the same height, their relative order in the final sorted sequence must remain the same as in the initial configuration.
inputFormat
The first line contains an integer (n) ((1 \leq n \leq 10^5)), representing the number of items. The second line contains (n) integers, where each integer indicates the height of an item.
outputFormat
Output (n) integers (P_1, P_2, \ldots, P_n) separated by spaces, where (P_i) is the position of the (i)th smallest item (considering stable order) in the current list before performing the (i)th reversal (i.e. reversing the segment from the (i)th position to (P_i)).
sample
6
4 5 3 1 6 2
4 6 6 5 5 6