European Girls' Olympiad in Informatics: 2021 Day 1 Problem 2
Luna came up with a wild idea. She lined up her
Luna wants to send each of the
There are two possible actions that Luna can take:
- She can swap any two friends who stand next to each other in the line.
- If a couple is standing next to each other in the line, Luna can send them on a date. This removes the couple from the line. The remaining friends then shift to fill in the gap in the line.
The actions can be performed in any order. E.g., she can make some swaps, then send some pairs of friends on a date, and then go back to making swaps.
Find and report the minimum number of actions needed to send everybody on a date.
Input Specification
The first line of the input contains a single integer
The second line of the input contains single-space separated integers
Output Specification
The first and only line of the output contains the minimum number of actions Luna must make in order to send every couple on a date.
Constraints
Subtask | Points | Constraints |
---|---|---|
For each couple there is no person between the two friends forming a couple, and |
||
For each couple there is at most one person between the two friends forming a couple, and |
||
The first |
||
The first |
||
Sample Input 1
3
3 1 2 1 2 3
Sample Output 1
4
Explanation for Sample Output 1
Luna could start by swapping the third and the fourth friend. After
this swap the line looks as follows:
Then, she can send the couple with number
Overall this solution takes
Sample Input 2
5
5 1 2 3 2 3 1 4 5 4
Sample Output 2
7
Comments