AcWing 3685. 求众数
原题链接
简单
作者:
tobequiet
,
2025-02-04 23:47:06
,
所有人可见
,
阅读 2
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int const N = 2e5 + 3,null = 0x3f3f3f3f;
int n;
int h[N],count[N];
int find(int x){
int k = (x % N + N) % N;
while(h[k] != null && h[k] != x){
k++;
if(k == N){
k = 0;
}
}
return k;
}
int main(){
scanf("%d",&n);
memset(h,0x3f,sizeof h);
while(n--){
int x;
scanf("%d",&x);
int k = find(x);
count[k] ++;
h[k] = x;
}
int tmp = -1,cnt = 0;
for(int i = 0; i < N; i++){
if(count[i] > cnt || count[i] == cnt && h[i] < tmp){
tmp = h[i];
cnt = count[i];
}
}
printf("%d\n",tmp);
return 0;
}