#P14096. [ICPC 2023 Seoul R] Walk Swapping

[ICPC 2023 Seoul R] Walk Swapping

题目描述

A cycle CC with nn vertices is a graph such that the vertices ii and i+1i+1, for i=1,,n1i=1,\dots,n-1, are connected by an edge and also the vertices nn and 11 are connected by an edge, where the vertices of CC are numbered 11 to nn.

There are nn coins each of which is numbered as one of {1,2,,n}\{1, 2,\dots, n\} and the numbers of two coins can be same. Initially, each vertex of CC has a coin among those coins. Then two vertices can swap their coins with each other. For a walk w=v1,v2.,vkw=v_1,v_2.\dots,v_k) in CC, a walk swapping is to swap two coins on viv_i and vi+1v_{i+1} in the order of i=1,2,,k1i=1,2,\dots,k-1. Here, a walk ww is a sequence of kk(1\ge 1) vertices whose consecutive two vertices are different and adjacent in CC, and it can be considered as the vertices visited when you traverse CC. Also, kk is called the length of ww. For a walk of length k2k\ge 2, k1k-1 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 (2,1,4,1)(2, 1, 4, 1) in the cycle with 44 vertices.

There are given the initial configuration of coins that the vertices of CC 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 (2,1,4,1)(2, 1, 4, 1) is 33, however, the final configuration of coins is also achieved by the walk swapping of the walk (1,2)(1, 2) with only one swap.

Given the number of vertices of a cycle CC 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 nn (1n3,0001\le n\le 3,000), where nn is the number of vertices in a cycle CC. The vertices are numbered from 11 to nn, and the coins on the vertices are numbered as ones of {1,2,,n}\{1, 2, \dots , n\} . The second line contains nn integers, allowed for duplication, between 11 and nn, where the ii-th integer is the coin lying on the vertex ii in the initial configuration of coins, for i=1,,ni=1,\dots, n. The third line contains nn integers, allowed for duplication, between 11 and nn, where the ii-th integer is the coin lying on the vertex ii in the final configuration of coins, for i=1,,ni=1,\dots,n.

输出格式

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