CF1215E Marbles
题目描述
有 $n (n \le 4 \times 10^5) $ 个珠子,第 $i$ 个珠子颜色是 $ c_i (c_i \le 20)$,每次操作把**相邻**的两个珠子交换。现在要把相同颜色的珠子排列在相连的一段,问至少要多少次操作。
输入格式
第一行:一个数 $n$。
第二行:$n$ 个数,表示珠子的颜色。
输出格式
一行:至少交换的次数。
说明/提示
In the first example three operations are enough. Firstly, Monocarp should swap the third and the fourth marbles, so the sequence of colors is $ [3, 4, 3, 2, 4, 2, 2] $ . Then Monocarp should swap the second and the third marbles, so the sequence is $ [3, 3, 4, 2, 4, 2, 2] $ . And finally, Monocarp should swap the fourth and the fifth marbles, so the sequence is $ [3, 3, 4, 4, 2, 2, 2] $ .
In the second example there's no need to perform any operations.