#P8637. [蓝桥杯 2016 省 B] 交换瓶子

[蓝桥杯 2016 省 B] 交换瓶子

Description

There are NN bottles, numbered 1N1 \sim N, placed on a shelf.

For example, there are 55 bottles:

2,1,3,5,42,1,3,5,4

Each time, you are allowed to pick up 22 bottles and swap their positions.

After several swaps, make the bottle numbers become:

1,2,3,4,51,2,3,4,5

For such a simple case, it is obvious that at least 22 swaps are needed to restore the order.

What if there are more bottles? You can solve it by programming.

Input Format

The first line: a positive integer NN (N<10000N<10000), representing the number of bottles.

The second line: NN positive integers separated by spaces, representing the current arrangement of the bottles.

Output Format

Output one positive integer in a single line, representing the minimum number of swaps needed to finish the sorting.

5
3 1 2 5 4
3
5
5 4 3 2 1
2

Hint

Time limit: 1 second, 256M. Lanqiao Cup 2016, the 7th provincial contest.

Lanqiao Cup 2016 provincial contest, Group B, Problem I.

Translated by ChatGPT 5