#FT0000065. Sắp xếp hai hàng que diêm (Matchstick Arrangement)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Sắp xếp hai hàng que diêm (Matchstick Arrangement)
Matchstick Arrangement
Source: Luogu
Version: Phuoc Hung OJ Extended
Problem Statement
There are two rows of matchsticks, each containing sticks. Stick in the first row has height and stick in the second row has height . All heights within each row are pairwise distinct.
The distance between the rows is
You may swap two adjacent sticks within the same row, at a cost of one operation. Swaps may be performed in either row.
First minimize the distance . Among all arrangements attaining its minimum, determine the minimum total number of adjacent swaps across both rows. Output this number, not the distance.
Input
The first line contains .
The second line contains integers , the first row heights from left to right.
The third line contains integers , the second row heights.
Output
Print one integer: the minimum number of adjacent swaps, modulo .
Subtasks
-
Subtask 1 (10 points): ; for each stick; all are distinct and all are distinct.
-
Subtask 2 (20 points): ; for each stick; all are distinct and all are distinct.
-
Subtask 3 (30 points): ; for each stick; all are distinct and all are distinct.
-
Subtask 4 (40 points): ; for each stick; all are distinct and all are distinct.
Examples
Input
4
2 3 1 4
3 2 1 4
Output
1
Explanation
Both rows contain the heights , so their minimum distance is , achieved when the height sequences match.
Initially the rows are and . Swapping the first two sticks of the first row makes both rows , attaining in one operation. Since the original rows differ, zero swaps cannot achieve . Hence the minimum is , and the modulo result is 1.