#C8746. Minimum Swaps to Sort Books

    ID: 52762 Type: Default 1000ms 256MiB

Minimum Swaps to Sort Books

Minimum Swaps to Sort Books

You are given a collection of books identified by unique integer IDs. The books are arranged in an unsorted order. Your task is to determine the minimum number of swaps required to sort the books in ascending order.

More formally, if the current order of books is given by an array \(a = [a_1, a_2, \dots, a_n]\) and the sorted order is \(b = [b_1, b_2, \dots, b_n]\) (where \(b\) is the sorted permutation of \(a\)), you need to compute the minimum number of swaps such that the array becomes sorted. Each swap can exchange any two elements. The optimal solution uses the cycle detection method to determine the number of swaps needed.

inputFormat

The first line of input contains an integer \(n\) \( (1 \leq n \leq 10^5)\) which denotes the number of books.

The second line contains \(n\) space-separated integers representing the IDs of the books.

outputFormat

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

## sample
5
4 3 1 5 2
4