#P1966. [NOIP 2013 提高组] 火柴排队
[NOIP 2013 提高组] 火柴排队
Description
Hanhan has two boxes of matches, each containing matches. Every match has a height. Now arrange the matches from each box into a column. Within the same column, all heights are distinct. Define the distance between the two columns as .
Here is the height of the -th match in the first column, and is the height of the -th match in the second column.
You may swap adjacent matches within each column. Please minimize the distance between the two columns by swapping. What is the minimum number of swaps needed to achieve this minimum distance? If this number is too large, output the result of this minimum number of swaps modulo .
Input Format
There are three lines in total. The first line contains an integer , the number of matches in each box.
The second line contains integers separated by single spaces, representing the heights of the matches in the first column.
The third line contains integers separated by single spaces, representing the heights of the matches in the second column.
Output Format
Output a single integer: the minimum number of swaps modulo .
4
2 3 1 4
3 2 1 4
1
4
1 3 4 2
1 7 2 4
2
Hint
Explanation for Sample 1:
The minimum distance is ; the minimum number of swaps is . For example, swap the first matches in column , or swap the first matches in column .
Explanation for Sample 2:
The minimum distance is ; the minimum number of swaps is . For example, swap the middle matches in column , and then swap the last matches in column .
Constraints
- For of the testdata, .
- For of the testdata, .
- For of the testdata, .
- For of the testdata, , , and for any , , .
Translated by ChatGPT 5
京公网安备 11011102002149号