| SMU ICPC Selection Contest 2026 |
|---|
| Finished |
A suspicious streamer named TLE Creators proposed an algorithm to sort a permutation. Note that the code below uses one-indexed positions.
Function CyclicSort(arr):
pos = 1
WHILE (arr is not sorted) DO
index = find the index of the minimum element in arr[pos...n]
shifts = index - pos
FOR (i = 1...shifts) DO
cyclically shift left arr[pos...n] by one position
END
pos = pos + 1
END
END
Let's consider an example by performing CyclicSort on the permutation $$$p = [2,4,1,3,5]$$$.
TLE Creators proved that CyclicSort always sorts any permutation in ascending order, but they are having difficulty determining the number of cyclic left-shift operations that CyclicSort will perform. They are going live right after your contest, and desperately need your help!
Given a permutation $$$p$$$ of length $$$n$$$, please help TLE Creators determine the number of cyclic left-shift operations that CyclicSort will perform.
The first line contains a single integer $$$t$$$ ($$$1\leq t \leq 10^3$$$), the number of test cases. For each test case,
$$$t$$$ lines. For each test case, output a single integer, the number of cyclic left-shift operations performed by CyclicSort.
452 4 1 3 562 3 1 4 5 666 5 4 3 2 171 2 3 4 5 6 7
6 5 15 0
There are $$$4$$$ test cases.
| Name |
|---|


