#P5426. [USACO19OPEN] Balancing Inversions G

[USACO19OPEN] Balancing Inversions G

Problem Description

Bessie and Elsie are playing a game on a boolean array AA of length 2N2N (1N1051 \leq N \leq 10^5). Bessie's score is the number of inversions in the first half of AA, and Elsie's score is the number of inversions in the second half of AA. An inversion is a pair of elements satisfying A[i]=1A[i] = 1 and A[j]=0A[j] = 0, where i<ji < j. For example, an array with a block of 00's followed by a block of 11's has no inversions, while an array with XX 11's followed by YY 00's has XYXY inversions.

Farmer John happens to see this board, and he wonders what the minimum number of adjacent swaps is to make the game appear tied. Please help Farmer John compute the answer.

Input Format

The first line contains NN. The second line contains 2N2N integers, each being 00 or 11.

Output Format

Output the minimum number of moves needed to make the game tied.

5
0 0 0 1 0 1 0 0 0 1
1

Hint

In this example, initially the first half has 11 inversion and the second half has 33 inversions. After swapping the 55-th and the 66-th numbers, both subarrays have 00 inversions.

Translated by ChatGPT 5