#K57717. Minimum Swaps to Sort Stones by Power
Minimum Swaps to Sort Stones by Power
Minimum Swaps to Sort Stones by Power
You are given an array of distinct integers representing the power values of stones. Your task is to determine the minimum number of swaps required to rearrange the stones in non-decreasing order.
The answer is computed based on cycles. For every cycle of misplaced elements, the number of swaps required is \(cycle\_size - 1\). In other words, the total number of swaps required is given by:
\( \text{Swaps} = \sum_{\text{cycle}} (\text{cycle\_size} - 1) \)
Read the input from standard input and output the result to standard output.
inputFormat
The first line contains a single integer \(n\) representing the number of stones. The second line contains \(n\) space-separated integers indicating the power values of the stones.
outputFormat
Output a single integer representing the minimum number of swaps required to arrange the stones in non-decreasing order.
## sample5
4 3 1 2 5
3