本文章由 WyOJ Shojo 从洛谷专栏拉取,原发布时间为 2022-04-14 20:41:46
题解思路:
有一个很显然的性质:
最后相同的元素一定是 $a_n$。
所以我们每次要尽可能多的去贴数字到前面。
比如:
$4\ 4\ 2\ 4\ 4$ 那么我们一定会把 $4\ 4$ 移过去,因为这样变成相同的元素才能尽可能的多。
AC CODE:
#include <iostream>
#include <cstring>
using namespace std;
const int N = 200010;
int a[N];
int main() {
int T;
cin >> T;
while (T --) {
int n;
cin >> n;
for (int i = 1; i <= n; ++ i)
cin >> a[i];
int p = n - 1 , ans = 0;
while (p > 0) {
if (a[p] != a[p + 1]) {
ans ++;
p -= n - p;
if (p < 0)
break;
a[p + 1] = a[n];\/\/把元素复制过来
}else -- p;
}
cout << ans << endl;
}
return 0;
}

鲁ICP备2025150228号