CF1954B Make It Ugly solution
Description
给定一个长度为
定义一个数组
定义一次操作为:选择任意一个
Solution
首先考虑一个数组是「美丽」的需要满足什么样的条件。
由题意可知,对数组的改变仅当三个连续元素形如
即:不存在两个(或多个)不同的
接下来,再考虑如何确定这个唯一的
于是形式化地,我们可以得出数组
对于数组
既然我们确定了数组
即
对于无解的情况,因为只要
Code
#include<iostream>
#include<cstdio>
#define il inline
#define inf (int)(2e9)
using namespace std;
const int MAXN=3e5+5;
int n;
int a[MAXN];
il void solve()
{
cin>>n;
for(int i=1;i<=n;++i)
cin>>a[i];
int k=a[1];
bool same=1;
for(int i=1;i<=n;++i)
{
if(a[i]!=k)
{
same=0;
break;
}
}
if(same)
{
cout<<-1<<'\n';
return;
}
int ans=inf,now=0;
for(int i=1;i<=n;++i)
{
if(a[i]==k)
++now;
else
{
if(now<ans)
ans=now;
now=0;
}
}
if(now<ans && now)
ans=now;
cout<<ans<<'\n';
return;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin>>T;
while(T--)
solve();
return 0;
}