#C9662. Minimum Swaps to Sort

    ID: 53780 Type: Default 1000ms 256MiB

Minimum Swaps to Sort

Minimum Swaps to Sort

This problem requires you to determine the minimal number of swaps required to sort an array of integers into increasing order. The array represents a sequence of production steps that are currently out of order. A swap is defined as exchanging two elements in the array. The objective is to compute the minimum number of such swaps needed to obtain a sorted sequence.

More formally, given an array \(a = [a_1, a_2, \dots, a_n]\), you need to determine the minimum number of swaps such that the resulting array is sorted in non-decreasing order.

Note: Use the cycle decomposition method where each cycle of misplaced elements contributes \(\text{cycle_size} - 1\) swaps to the total number of swaps.

inputFormat

The input is provided via standard input (stdin). The first line contains an integer (n) (the number of steps). The second line contains (n) space-separated integers representing the current order of the production steps.

outputFormat

Output a single integer to standard output (stdout) which represents the minimal number of swaps needed to sort the array in increasing order.## sample

5
3 1 2 5 4
3