AmazonOA #256
Make Value Groups Contiguous
估计中等可在线判题
One operation chooses two different values x and y and replaces every x in the array with y simultaneously. In the end, all occurrences of each value must lie in one contiguous block; find the minimum number of operations. You cannot change just one occurrence, nor delete elements.
输入格式
The first line contains n; the second line contains the array. This site uses 1≤n≤200000, −10^9≤element≤10^9.
输出格式
Output a single integer, the answer.
样例 1
输入:
4
1 2 3 1
输出:
2
样例 2
输入:
3
1 2 3
输出:
0
样例 3
输入:
6
1 2 1 3 4 3
输出:
2
样例说明
The answers are 2, 0 and 2. The first example is not only about handling the repeated 1s; the 2 and 3 sandwiched between the two 1s must also be merged in the end.