#C6537. Minimum Swaps to Sort

    ID: 50308 Type: Default 1000ms 256MiB

Minimum Swaps to Sort

Minimum Swaps to Sort

You are given a list of integers representing the heights of books. Your task is to compute the minimum number of swaps required to arrange the books in non-decreasing order. In a single swap, you can exchange the positions of any two books.

The key observation is that if you represent the unsorted list as a permutation of the sorted list, the minimal number of swaps required to fix a cycle of length \(L\) is \(L-1\). Hence, the total number of swaps is the sum over all cycles.

inputFormat

The input is given via standard input. The first line contains an integer \(n\) which represents the number of books. The second line contains \(n\) space-separated integers representing the heights of the books.

outputFormat

Output a single integer denoting the minimum number of swaps required to sort the books in non-decreasing order. The output should be printed to standard output.

## sample
5
4 3 1 2 5
3