#P10068. [CCO 2023] Line Town
[CCO 2023] Line Town
Problem Description
The residents of Line Town are standing in a line. Initially, from left to right, their happiness values are .
You are the mayor of Line Town, and you are carrying out your plan called "Community, Candy, and Organization" (CCO). Therefore, you have the power to swap residents' positions. In one swap, you may swap two adjacent residents in the line. However, this swap will flip (negate) the happiness values of both residents.
You want to know whether it is possible to perform some swaps so that the residents' happiness values, from left to right, are in non-decreasing order. If it is possible, output the minimum number of swaps required.
Input Format
The first line contains an integer .
The second line contains integers , representing the residents' happiness values from left to right.
Output Format
Output one integer on a single line: the minimum number of swaps. If the task is impossible, output .
6
-2 7 -1 -8 2 8
3
4
1 -1 1 -1
-1
Hint
Sample Explanation 1
You can perform swaps as follows:
- Swap the 2nd and 3rd residents, and the happiness values become .
- Swap the 4th and 5th residents, and the happiness values become .
- Swap the 3rd and 4th residents, and the happiness values become .
The residents are now in non-decreasing order of happiness values as required. It is impossible to achieve a non-decreasing arrangement with fewer than swaps.
Sample Explanation 2
There is no sequence of swaps that can make the residents arranged in non-decreasing order of happiness values.
Constraints
For all testdata, , .
- Additional Constraint A: For all , .
- Additional Constraint B: For all , .
- Additional Constraint C: For all , .
| Subtask ID | Score | Range of | Additional Constraint |
|---|---|---|---|
| 1 | 12 | A | |
| 2 | |||
| 3 | B | ||
| 4 | 16 | ||
| 5 | C | ||
| 6 | 12 | ||
| 7 | 8 | ||
| 8 | 12 |
Translated by ChatGPT 5