bzoj2456: mode
用抵消的思想好神啊这个。
#include<cstdio>
int n,ans,cnt,u;
int main(){
scanf("%d",&n);
while(n--){
scanf("%d",&u);
if(!cnt) ans=u,cnt=1;
else if(u==ans) cnt++;
else cnt--;
}
printf("%d\n",ans);
return 0;
}
2456: mode
Time Limit: 1 Sec Memory Limit: 1 MBSubmit: 3594 Solved: 1510
[Submit][Status][Discuss]
Description
给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数。
Input
第1行一个正整数n。
第2行n个正整数用空格隔开。
Output
一行一个正整数表示那个众数。
Sample Input
5
3 2 3 1 3
3 2 3 1 3
Sample Output
3
HINT
100%的数据,n<=500000,数列中每个数<=maxlongint。
zju2132 The Most Frequent Number

浙公网安备 33010602011771号