Balancing Inversions
Time limit2sMemory limit512 MB
Given a binary array of length 2N, find the fewest adjacent swaps so that the inversion counts of its first and second halves become equal.
Problem
Bessie and Elsie were playing a game on a boolean array of length (). Bessie's score was the number of inversions in the first half of , and Elsie's score was the number of inversions in the second half of . An inversion is a pair of entries and where . For example, an array consisting of a block of 0s followed by a block of 1s has no inversions, and an array consisting of a block of 1s followed by a block of 0s has inversions.
Farmer John has found the game board and wants to know the minimum number of swaps between adjacent elements needed so that the game ends in a tie. Help him compute this number.
Input
The first line contains . The next line contains integers, each either 0 or 1.
Output
Print the minimum number of adjacent swaps needed to make the scores tied.
Hint
In the example, the first half of the array initially has 1 inversion, and the second half has 3 inversions. After swapping the 5th and 6th bits, both halves have 0 inversions.