#P8637. Minimum Bottle Swap Operations
Minimum Bottle Swap Operations
Minimum Bottle Swap Operations
There are N bottles, numbered from 1 to N, arranged on a shelf. You are allowed to choose exactly two bottles and swap their positions in a single operation. The goal is to determine the minimum number of swap operations needed to sort the bottles in increasing order.
For example, if there are 5 bottles arranged as: $$2,1,3,5,4$$, by swapping bottles appropriately, the order can be transformed into: $$1,2,3,4,5$$. In this case, at least 2 swaps are required.
More formally, let the initial permutation be $$a_1, a_2, \dots, a_N$$. Your task is to compute the minimum number of pairwise swaps to rearrange the permutation into ascending order: $$1,2,\dots,N$$.
inputFormat
The input consists of two lines:
The first line contains an integer $$N$$, the number of bottles.
The second line contains $$N$$ space-separated integers representing the current arrangement of the bottles.
outputFormat
Output a single integer — the minimum number of swap operations required to sort the bottles in increasing order.
sample
5
2 1 3 5 4
2
</p>