#P12406. 「CZOI-R3」消除序列
「CZOI-R3」消除序列
Problem Description
There are two permutations of length . You can perform the following operations any number of times:
- Cyclically shift left by one position. If before the operation, it costs .
- Cyclically shift right by one position. If before the operation, it costs .
- Swap and . It costs .
- If , cyclically shift left by one position, and set . This costs nothing.
Find the minimum total cost to make for all . It is guaranteed that the goal can always be achieved by some sequence of operations.
: Suppose before a cyclic left shift the sequence is , then after the operation it becomes . Suppose before a cyclic right shift the sequence is , then after the operation it becomes .
Input Format
The first line contains four integers .
The second line contains integers, representing the permutation .
The third line contains integers, representing the permutation .
Output Format
Output one integer in one line, representing the answer.
2 1 1 1
1 2
2 1
1
5 4 3 2
1 4 3 2 5
5 1 4 2 3
3
Hint
Constraints
This problem uses bundled testdata.
- Subtask #1 (): .
- Subtask #2 (): .
- Subtask #3 (): . Depends on Subtask #1.
- Subtask #4 (): No special properties. Depends on Subtask #2 #3.
For of the testdata: , and are permutations of length . .
Translated by ChatGPT 5