308386: CF1510K. King's Task

Memory Limit:512 MB Time Limit:3 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

King's Task

题意翻译

给你一个长度为 $2n$ 的数列,你每次可以进行一下两种操作之一: 1. 将所有的第 $2k$ 位和第 $2k-1$ 位交换($1\leq k \leq n$)。 2. 将所有的第 $k$ 位和第 $k+n$ 位交换($1\leq k \leq n$)。 试求使给定序列变有序的最小操作次数。

题目描述

The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task. There is a permutation $ p_i $ of numbers from 1 to $ 2n $ . You can make two types of operations. 1. Swap $ p_1 $ and $ p_2 $ , $ p_3 $ and $ p_4 $ , ..., $ p_{2n-1} $ and $ p_{2n} $ . 2. Swap $ p_1 $ and $ p_{n+1} $ , $ p_2 $ and $ p_{n+2} $ , ..., $ p_{n} $ and $ p_{2n} $ . The task is to find the minimal number of operations required to sort the given permutation. The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King's task.

输入输出格式

输入格式


The first line contains the integer $ n $ ( $ 1\le n\le 1000 $ ). The second line contains $ 2n $ integers $ p_i $ — the permutation of numbers from 1 to $ 2n $ .

输出格式


Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print $ -1 $ .

输入输出样例

输入样例 #1

3
6 3 2 5 4 1

输出样例 #1

3

输入样例 #2

2
3 4 2 1

输出样例 #2

-1

输入样例 #3

4
1 2 3 4 5 6 7 8

输出样例 #3

0

说明

In the first example, you can sort the permutation in three operations: 1. Make operation 1: $ 3, 6, 5, 2, 1, 4 $ . 2. Make operation 2: $ 2, 1, 4, 3, 6, 5 $ . 3. Make operation 1: $ 1, 2, 3, 4, 5, 6 $ .

Input

题意翻译

给你一个长度为 $2n$ 的数列,你每次可以进行一下两种操作之一: 1. 将所有的第 $2k$ 位和第 $2k-1$ 位交换($1\leq k \leq n$)。 2. 将所有的第 $k$ 位和第 $k+n$ 位交换($1\leq k \leq n$)。 试求使给定序列变有序的最小操作次数。

加入题单

算法标签: