#P6339. Staged Adjacent Swap Sorting
Staged Adjacent Swap Sorting
Staged Adjacent Swap Sorting
You are given a permutation of the numbers \(1\) through \(n\) of length \(n\). Your task is to sort the permutation in ascending order by performing a sequence of adjacent swaps in multiple stages.
The sorting procedure is divided into stages as follows:
- In the first stage, move the number \(1\) to the first position (index 1) by repeatedly swapping it with an adjacent element.
- In the second stage, move the number \(n\) to the last position (index \(n\)) by the same method.
- In the third stage, move the number \(2\) to the second position.
- In the fourth stage, move the number \(n-1\) to the \(n-1\)th position.
- Continue in this alternating fashion until every number is moved to its correct sorted position.
For each stage, output the number of adjacent swaps performed.
Note: Throughout the process, the array is modified by the swaps made in earlier stages.
inputFormat
The input consists of two lines.
- The first line contains an integer \(n\) (\(1 \le n \le 10^5\)) representing the size of the permutation.
- The second line contains \(n\) space-separated integers representing a permutation of \(1\) through \(n\).
outputFormat
For each stage in the process, print a line containing a single integer which represents the number of adjacent swaps performed in that stage.
sample
4
1 3 4 2
0
1
1
0
</p>