#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mxn = 2e5+5;
int cnt[mxn],b[mxn],a[mxn],a2[mxn];
int len,k,n,ans;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(cnt,0,sizeof(cnt));
len=k=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]); //读入序列a
if(a[i]!=a[i-1])
{ //压缩序列a2[]
k++;
a2[k]=a[i];
}
}
for(int i=1;i<=k;i++)
{
if(!cnt[a2[i]])
{ //特判第一次出现
if(i>1) //不在开头,左侧有数组,划分段数++
cnt[a2[i]]++; //第一次出现过了
len++; //第一次出现则加入最终的答案筛选数组;
b[len]=a2[i];
}
if(i<k) //不在结尾,划分段数++;***如开头:它不在结尾*****
cnt[a2[i]]++; //若非第一次出现,之前第一次出现已经加过来 ,不用b[len]
}
ans=cnt[b[1]]; //cnt:划分段数 b:筛选数组
for(int i=2;i<=len;i++)
{
ans=ans<cnt[b[i]] ?ans:cnt[b[i]];
}
printf("%d\n",ans);
}
return 0;
}
C
C. Sequence Transformation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a sequence a�, initially consisting of n� integers.
You want to transform this sequence so that all elements in it are equal (i. e. it contains several occurrences of the same element).
To achieve this, you choose some integer x� that occurs at least once in a�, and then perform the following operation any number of times (possibly zero): choose some segment [l,r][�,�] of the sequence and remove it. But there is one exception: you are not allowed to choose a segment that contains x�. More formally, you choose some contiguous subsequence [al,al+1,…,ar][��,��+1,…,��] such that ai≠x��≠� if l≤i≤r�≤�≤�, and remove it. After removal, the numbering of elements to the right of the removed segment changes: the element that was the (r+1)(�+1)-th is now l�-th, the element that was (r+2)(�+2)-th is now (l+1)(�+1)-th, and so on (i. e. the remaining sequence just collapses).