Canadian Computing Olympiad: 2023 Day 1, Problem 3
The
Since you are the mayor of Line Town, you are implementing the third pillar of your plan entitled "Community, Candy, and Organization" (CCO). As such, you have taken the mayoral power to swap the residents' locations. In one swap, you may tell two adjacent residents to swap their positions in the line. However, this swap will cause both residents to negate their happiness values.
You would like to perform some swaps so that the residents' happiness values are in nondecreasing order from left to right in the line. Determine whether this is possible, and if so, the minimum number of swaps needed.
Input Specification
The first line of input contains a single integer
The next line of input contains
Marks Awarded | Bounds on | Bounds on |
---|---|---|
No additional constraints. | ||
No additional constraints. |
Output Specification
On a single line, output the minimum number of swaps, or -1
if the task is impossible.
Sample Input 1
6
-2 7 -1 -8 2 8
Output for Sample Input 1
3
Explanation of Output for Sample Input 1
It is possible to perform
- Swap the
and resident so that the line becomes . - Swap the
and resident so that the line becomes . - Swap the
and resident so that the line becomes .
The residents are now arranged in non-decreasing order of happiness values as required. No
non-decreasing arrangement can be obtained with less than
Sample Input 2
4
1 -1 1 -1
Output for Sample Input 2
-1
Explanation of Output for Sample Input 2
There is no sequence of swaps that will place residents in non-decreasing order of happiness values.
Comments