跳到正文

题解

CF1931C题解

Vitamin_B约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年2月20日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 1 分钟
题目
CF1931C

版本

  1. v1当前

思路

题目中说了“至多一次操作”,所以只有左边连续一段和右边连续一段不可能被改。这时候分情况讨论:

  1. a1=a2=a3=⋯=ana_1=a_2=a_3=\dots=a_n,答案是 00;
  2. a1=ana_1=a_n,答案是 nn 减去头尾的连续段;
  3. a1≠ana_1\ne a_n,答案是 n−max⁡(开头的连续段,结尾的连续段)n-\max (\text{开头的连续段},\text{结尾的连续段})。

代码

C++
# include <bits/stdc++.h>
using namespace std;
int t, n, a[200005], sum1, sum2, i;
int main () {
	ios::sync_with_stdio (0);
	cin.tie (0);
	cout.tie (0);
	cin >> t;
	while (t --) {
		cin >> n;
		i = 1, sum1 = sum2 = 0;
		for (int i = 1; i <= n; ++ i)
			cin >> a[i];
		while (i <= n && a[i] == a[1])
			++ i, ++ sum1;
		if (i > n) {
			cout << "0\n";
			continue ;
		}
		i = n;
		while (a[i] == a[n])
			-- i, ++ sum2;
		cout << n - (a[1] == a[n] ? sum1 + sum2 : max (sum1, sum2)) << '\n';
	}
	return 0;
}
原文