#C3695. Minimum Swaps to Sort Books

    ID: 47150 Type: Default 1000ms 256MiB

Minimum Swaps to Sort Books

Minimum Swaps to Sort Books

You are given a list of books where each book is represented by an integer denoting the number of pages. Your task is to determine the minimum number of swaps required to sort the books in ascending order of page numbers.

To solve this problem, you may think of the list as a permutation of elements. By identifying cycles in this permutation, the minimum number of swaps needed is given by the sum over each cycle of (cycle length - 1). In mathematical notation, if the permutation decomposes into cycles, the number of swaps required is:

\( \text{Minimum Swaps} = \sum_{\text{each cycle}} (\text{cycle length} - 1) \)

Implement an efficient solution to compute this value.

inputFormat

The first line contains a single integer (n) denoting the number of books. The second line contains (n) space-separated integers representing the number of pages in each book.

outputFormat

Output a single integer representing the minimum number of swaps required to sort the books in ascending order.## sample

5
3 1 5 2 4
4

</p>