#P5749. [IOI 2019] 排列鞋子

[IOI 2019] 排列鞋子

Description

Adnan owns the largest shoe store in Baku. A box containing nn pairs of shoes has just arrived at his store. Each pair of shoes consists of two shoes of the same size: one left shoe and one right shoe. Adnan lines up these 2n2n shoes in a row. This row has a total of 2n2n positions, numbered from 00 to 2n12n-1 from left to right.

Adnan wants to rearrange these shoes into a valid permutation. A permutation is valid if and only if for all i(0in1)i(0\leqslant i \leqslant n - 1), the following conditions all hold:

  • The shoes at positions 2i2i and 2i+12i+1 have the same size.
  • The shoe at position 2i2i is a left shoe.
  • The shoe at position 2i+12i+1 is a right shoe.

To achieve this, Adnan can perform a sequence of swaps. In each swap, he chooses two currently adjacent shoes and swaps them (that is, he picks them up and puts each shoe back into the other shoe’s original position). Two shoes are adjacent if and only if the difference of their position indices is 11.

Find the minimum number of swaps Adnan needs to obtain a valid permutation.

Input Format

The first line contains a positive integer nn, meaning there are nn pairs of shoes.

The second line contains 2n2n integers SiS_i. The ii-th integer describes the shoe whose position index is i1i-1. Here Su0|S_u|\neq 0, and it equals the size of the shoe initially at position ii. Here x|x| denotes the absolute value of xx: it equals xx when x0x\geq 0, and equals x-x when x<0x < 0. If Si<0S_i < 0, then the shoe at position ii is a left shoe; otherwise, it is a right shoe.

Output Format

Output one line with one integer, the minimum number of swaps.

2
2 1 -1 -2

4

3
-2 2 2 -2 -2 2

1

Hint

Explanation for Sample 1

Adnan can obtain a valid permutation with 44 swaps.

For example, he can first swap 11 and 1-1, then swap 11 and 2-2, then swap 1-1 and 2-2. Finally, swap 2-2 and 22. After that, he can get a valid permutation. It is impossible to obtain a valid permutation with fewer than 44 swaps, so the output is 44.

Explanation for Sample 2

Adnan can swap the shoes at positions 22 and 33 to obtain the valid permutation [2,2,2,2,2,2][-2,2,-2,2,-2,2], so the output should be 11.

Constraints

For all testdata:

  • 1n1051\leqslant n\leqslant10^5.
  • For all i(0i2n1)i(0\leqslant i\leqslant 2n-1), 1Si+1n1\leqslant \left|S_{i+1}\right|\leqslant n.
  • A valid permutation can always be obtained through a sequence of swaps.

The detailed extra constraints and scores for subtasks are shown in the table below:

Subtask ID Additional Constraints Score
11 n=1n=1 1010
22 n8n\leqslant8 2020
33 All shoes have the same size.
44 All shoes at positions 0,,n10,\dots,n-1 are left shoes, and all shoes at positions n,,2n1n,\dots,2n-1 are right shoes. Also, for all i(0in1)i(0\leqslant i\leqslant n-1), the shoes at positions ii and i+ni+n have the same size. 1515
55 n103n\leqslant10^3 2020
66 No additional constraints. 1515

Translated by ChatGPT 5