Cranes
Time limit1sMemory limit128 MB
Given a permutation, each time unit lets several disjoint swaps happen at once; find the minimum number of time units to sort it.
- Level
Hard8 of 10
- Topics
- Graph, Greedy, Math, Combinatorics
- Solved
- No attempts yet
Problem
The BeczkoBit factory produces barrels for pickling bytes. The production line is fully automated, and the bottleneck is the warehouse. In the warehouse the finished barrels must be lined up in order of size, from the smallest to the largest. All barrels have distinct sizes.
The sorting is carried out by a team of cranes. Each crane can swap the positions of any two barrels. The cranes work in parallel, and a single swap by one crane takes unit of time. During any one unit of time each barrel may be moved by at most one crane, so two swaps performed in the same unit of time may not touch the same barrel.
Find the minimum time (number of time units) needed to arrange the barrels in ascending order of size.
Input
The first line contains an integer (), the number of barrels. The second line contains distinct integers from , giving the sizes of the barrels in the order they stand in the warehouse. The barrel in the first position is described by the first number, and the barrel in the last position by the last number.
Output
Print a single integer on one line: the minimum number of time units needed to sort the barrels into ascending order of size.