#P1966. [NOIP 2013 提高组] 火柴排队

[NOIP 2013 提高组] 火柴排队

Description

Hanhan has two boxes of matches, each containing nn 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 (aibi)2 \sum (a_i-b_i)^2.

Here aia_i is the height of the ii-th match in the first column, and bib_i is the height of the ii-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 108310^8-3.

Input Format

There are three lines in total. The first line contains an integer nn, the number of matches in each box.

The second line contains nn integers separated by single spaces, representing the heights of the matches in the first column.

The third line contains nn 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 108310^8-3.

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 0 0; the minimum number of swaps is 11. For example, swap the first 2 2 matches in column 11, or swap the first 22 matches in column 22.

Explanation for Sample 2:

The minimum distance is 1010; the minimum number of swaps is 22. For example, swap the middle 22 matches in column 11, and then swap the last 22 matches in column 22.

Constraints

  • For 10%10\% of the testdata, 1n101 \leq n \leq 10.
  • For 30%30\% of the testdata, 1n1001 \leq n \leq 100.
  • For 60%60\% of the testdata, 1n1031 \leq n \leq 10^3.
  • For 100%100\% of the testdata, 1n1051 \leq n \leq 10^5, 0ai,bi<2310 \leq a_i,b_i < 2^{31}, and for any 1i<jn1 \le i < j \le n, aiaja_i \neq a_j, bibjb_i \neq b_j.

Translated by ChatGPT 5