Problem 980
Minimum Deletions to Make Array Beautiful
Medium · 20 pts · Array, Greedy, Stack
Problem
An array is beautiful if its length is even and A[i] != A[i + 1] for every even index i (so each pair at positions 0-1, 2-3, ... has two different values). An empty array is beautiful. Delete the fewest elements to make the array beautiful; after a deletion, the elements to the right shift left. Build the result greedily from left to right, skipping an element when it would form an equal pair, and delete one more at the end if the length is odd.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].
Output
Print the minimum number of deletions.
Constraints
1 <= N <= 100000; 0 <= A[i] <= 10^5
Example input
5 1 1 2 3 5
Example output
1
Explanation
In 1 1 2 3 5, deleting the first 1 gives 1 2 3 5, whose pairs (1,2) and (3,5) both differ. One deletion is enough.