#C11055. Minimum Swaps to Increasing Sequence
Minimum Swaps to Increasing Sequence
Minimum Swaps to Increasing Sequence
You are given an array of n distinct integers. Your task is to determine the minimum number of swaps required to transform the array into a strictly increasing sequence (i.e. sorted in increasing order).
You are allowed to swap any two elements in the array. The goal is to achieve the sorted order with the fewest possible swaps.
Note: It is guaranteed that the array elements are unique.
Observation: If an element is part of a cycle of length L in the permutation mapping from the current order to the sorted order, then the number of swaps needed for that cycle is \(L-1\).
inputFormat
The input is read from stdin
and is structured as follows:
- The first line contains an integer n, representing the number of elements in the array.
- The second line contains n space-separated integers which are the elements of the array.
outputFormat
Output to stdout
a single integer — the minimum number of swaps required to transform the given array into a strictly increasing (sorted) sequence.
5
4 3 1 2 5
3