#P14096. [ICPC 2023 Seoul R] Walk Swapping
[ICPC 2023 Seoul R] Walk Swapping
题目描述
A cycle with vertices is a graph such that the vertices and , for , are connected by an edge and also the vertices and are connected by an edge, where the vertices of are numbered to .
There are coins each of which is numbered as one of and the numbers of two coins can be same. Initially, each vertex of has a coin among those coins. Then two vertices can swap their coins with each other. For a walk ) in , a walk swapping is to swap two coins on and in the order of . Here, a walk is a sequence of () vertices whose consecutive two vertices are different and adjacent in , and it can be considered as the vertices visited when you traverse . Also, is called the length of . For a walk of length , swaps in its walk swapping occur, and for a walk of length one, there is no swap. The figure below shows the progress of the walk swapping for the walk in the cycle with vertices.
There are given the initial configuration of coins that the vertices of have in the first time and the final configuration of coins that the vertices should have in the end. You have to find a walk swapping that results in the final configuration of coins from the initial one, minimizing the total number of swaps of coins in the walk swapping.
For example, in the above figure, the number of swaps of coins in the walk swapping of the walk is , however, the final configuration of coins is also achieved by the walk swapping of the walk with only one swap.
Given the number of vertices of a cycle and the initial and final configurations of coins on the vertices, write a program to output the minimum number of swaps of coins in a walk swapping resulting in the final configuration of coins from the initial one.
输入格式
Your program is to read from standard input. The input starts with a line containing one integer (), where is the number of vertices in a cycle . The vertices are numbered from to , and the coins on the vertices are numbered as ones of . The second line contains integers, allowed for duplication, between and , where the -th integer is the coin lying on the vertex in the initial configuration of coins, for . The third line contains integers, allowed for duplication, between and , where the -th integer is the coin lying on the vertex in the final configuration of coins, for .
输出格式
Your program is to write to standard output. Print exactly one line. The line should contain the minimum number of swaps of coins in a walk swapping that results in the final configuration of coins from the initial one. If there is no such walk swapping, that is, it is impossible to result in the final configuration by any walk swapping, print -1
.
4
4 3 2 1
3 4 2 1
1
6
2 1 1 2 2 1
1 2 2 2 1 1
7
6
4 1 3 6 2 5
6 2 1 3 4 5
-1