#P4402. Reversal Sorting
Reversal Sorting
Reversal Sorting
SORT Corporation provides a unique sorting service. They sort an array of items using only a specific sequence of reversal operations. The procedure is as follows:
For an array of n items (indexed from 1 to n), for each index \(i\) from 1 to \(n\), perform the following operation:
[ P_i = \min{ j \mid i \leq j \leq n \text{ and } A_j = \min(A[i], A[i+1], \ldots, A[n]) } ]
Then, reverse the subarray from index \(i\) to index \(P_i\) (inclusive). In case of duplicate values the one with the smaller index (i.e. the one appearing earlier in the original array) is considered first. After applying these reversals sequentially, the array becomes sorted in non-decreasing order.
Your task is to simulate this process and output the resulting sorted array.
inputFormat
The first line contains an integer \(n\) (\(1 \le n \le 10^5\)), representing the number of items.
The second line contains \(n\) integers separated by spaces, which represent the numbers on the items.
outputFormat
Output the final sorted array after performing the reversal operations. The numbers should be separated by spaces.
sample
6
4 6 5 1 2 3
1 2 3 4 5 6