#FT0000156. Bò qua đường – Số giao nhau ít nhất (Why Did the Cow Cross the Road (Minimum Crossings))
Bài tập thuộc lộ trình
Mục lục đề ⌄
Bò qua đường – Số giao nhau ít nhất (Why Did the Cow Cross the Road (Minimum Crossings))
Why Did the Cow Cross the Road (Minimum Crossings)
Source: USACO
Version: Phuoc Hung OJ Extended
Problem Statement
Two sides of a road each contain exactly one field for every breed , in the permutation orders and . Connect the fields of the same breed across the road. Two connections cross if their relative order is reversed between the sides. You may apply any cyclic shift to exactly one side (including no shift); arbitrary rearrangements or simultaneous shifts of both sides are not allowed. Find the minimum number of crossing pairs. Either side may yield the optimum.
Input
The first line contains . The next integers form permutation ; the following integers form permutation . The original USACO format puts each integer on its own line.
Output
Print a 64-bit integer: the minimum number of crossing pairs.
Subtasks
- Subtask 1 (20 points): ; Both sequences and are permutations of .
- Subtask 2 (30 points): ; Both sequences and are permutations of .
- Subtask 3 (50 points): ; Both sequences and are permutations of .
Examples
Example 1
Input
5
5
4
1
3
2
1
3
2
5
4
Output
0
Explanation
Side is and side is . Rotating left by two positions produces , exactly matching , so no connections cross and the optimum is 0.
Example 2
Input
3
1
2
3
3
2
1
Output
1
Explanation
Initially all three pairs are inverted. A one-position rotation yields orders and , with exactly one inversion. No rotation can make the count zero.