P29050 [洛谷] [IOI2019] Arranging Shoes
提高
题目描述

Problem Description

Due to the data point limitations on Luogu, this problem only evaluates 100 data points.

If you need to test all data, there are two problems:

  1. Subtask 1-3
  2. Subtask 4-6

Both problems are worth 50 points each, covering all test cases in their respective subtasks.


Adnan owns the largest shoe store in Baku. A box containing \( n \) pairs of shoes has just arrived at his store. Each pair consists of two shoes of the same size: one left shoe and one right shoe. Adnan arranges these \( 2n \) shoes in a row, which has a total of \( 2n \) positions, numbered from 0 to \( 2n-1 \) from left to right.

Adnan wants to rearrange these shoes into a valid arrangement. An arrangement is valid if and only if for all \( i \) ( \( 0 \leq i \leq n-1 \) ), the following conditions are met:

  • The shoes at positions \( 2i \) and \( 2i+1 \) are of the same size;
  • The shoe at position \( 2i \) is a left shoe;
  • The shoe at position \( 2i+1 \) is a right shoe.

To achieve this, Adnan can perform a series of swaps. In each swap, he selects two adjacent shoes to swap (i.e., he picks them up and places each shoe in the other shoe's original position). Two shoes are adjacent if and only if the difference between their position numbers is 1.

Determine the minimum number of swaps Adnan needs to make to achieve a valid arrangement.

Input Format

The first line contains a positive integer \( n \), representing the number of pairs of shoes.

The second line contains \( 2n \) integers \( S_i \), where the \( i \)-th integer represents the shoe at position \( i-1 \). Here, \( |S_u| \neq 0 \) and equals the size of the shoe initially at position \( i \). The absolute value \( |x| \) is \( x \) if \( x \geq 0 \) and \( -x \) if \( x < 0 \). If \( S_i < 0 \), the shoe at position \( i \) is a left shoe; otherwise, it is a right shoe.

Output Format

Output a single integer, representing the minimum number of swaps required.

Sample Input and Output

Sample Input #1

2
2 1 -1 -2

Sample Output #1

4

Sample Input #2

3
-2 2 2 -2 -2 2

Sample Output #2

1

Notes/Hints

Sample Explanation 1

Adnan can achieve a valid arrangement with 4 swaps.

For example, he can first swap \( 1 \) and \( -1 \), then swap \( 1 \) and \( -2 \), then swap \( -1 \) and \( -2 \). Finally, swap \( -2 \) and \( 2 \). It is not possible to achieve a valid arrangement with fewer than 4 swaps, so the output is 4.

Image

Sample Explanation 2

Adnan can achieve a valid arrangement by swapping the shoes at positions 2 and 3 to get the arrangement \([-2, 2, -2, 2, -2, 2]\), so the output is 1.

Data Range

For all data:

  • \( 1 \leq n \leq 10^5 \);
  • For all \( i \) ( \( 0 \leq i \leq 2n-1 \) ), \( 1 \leq |S_{i+1}| \leq n \);
  • There is always a valid arrangement that can be achieved through a series of swaps.

Detailed subtask additional restrictions and scores are as follows:

Subtask Number Additional Restrictions Score
1 \( n = 1 \) 10
2 \( n \leq 8 \) 20
3 All shoes are of the same size 20
4 All shoes at positions \( 0, \dots, n-1 \) are left shoes, and all shoes at positions \( n, \dots, 2n-1 \) are right shoes. Also, for all \( i \) ( \( 0 \leq i \leq n-1 \) ), the shoes at positions \( i \) and \( i+n \) are of the same size 15
5 \( n \leq 10^3 \) 20
6 No additional restrictions 15
输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
2 2 1 -1 -2
样例输出
4