思路
我们发现题面中的 ,数据十分小,可以利用这一点解题。我们用 表示 总共出现的次数,再用 表示 在 中的次数。
然后,用循环变量 ,遍历 ,先将 与 按照新的 更新。然后判断 中的第 个点,第 个点,以及 中的最大值 以及 的点是否满足题目条件即可。
过程
读入 ,。
循环变量 遍历 到 :
更新 与 ,即:,,。
然后更新最大值 :。
判断,如果满足以下任意一条,则将答案更新为 :
- 或 ;
- 且 ;
- 且 ;
输出答案。
注意事项
- 注意特判 的情况。
AC CODE
#include<bits/stdc++.h>
using namespace std;
int read(){int x=0;char f=1,ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
const int N=1e5+10;
int a[N],sum[N],cnt[20];
int main(){
int n=read();
if(n==1)
return printf("1\n"),0;
for(int i=1;i<=n;++i)
a[i]=read();
--sum[cnt[a[1]]],++cnt[a[1]],++sum[cnt[a[1]]];
int maxx=cnt[a[1]],ans=0;
for(int i=2;i<=n;++i){
--sum[cnt[a[i]]],++cnt[a[i]],++sum[cnt[a[i]]];
maxx=max(maxx,cnt[a[i]]);
if(sum[i]==1||sum[1]==i||sum[1]==1&&sum[maxx]*maxx==i-1||sum[maxx]==1&&sum[maxx-1]*(maxx-1)+maxx==i)
ans=i;
}
printf("%d\n",ans);
return 0;
}