King's Task
InterviewTime limit3sMemory limit512 MB
Given a permutation of 1 to 2n, find the fewest swaps of adjacent pairs or of the two halves needed to sort it, or report that sorting is impossible.
- Level
Medium6 of 10
- Topics
- Graph, BFS, Simulation, Implementation
- Solved
- No attempts yet
Problem
A brave knight came to the king and asked for permission to marry the princess. The king knew the knight was brave, but he also wanted to know whether he was smart enough. So he gave him the following task.
There is a permutation of the numbers from 1 to . You can perform two types of operations.
- Swap and , and , ..., and .
- Swap and , and , ..., and .
The task is to find the minimum number of operations needed to sort the given permutation.
The knight was not that smart, but he was charming, so the princess asks you to help him solve the king's task.
Input
The first line contains the integer (). The second line contains integers , the permutation of the numbers from 1 to .
Output
Print one integer, the minimum number of operations needed to sort the permutation. If the permutation cannot be sorted with these operations, print .
Hint
In the first example, the permutation can be sorted in three operations:
- Perform operation 1: .
- Perform operation 2: .
- Perform operation 1: .