http://www.elijahqi.win/2018/01/11/bzoj2456-mode/
Description
给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数。
Input
第1行一个正整数n。
第2行n个正整数用空格隔开。
Output
一行一个正整数表示那个众数。
Sample Input
5
3 2 3 1 3
Sample Output
3
HINT
100%的数据,n<=500000,数列中每个数<=maxlongint。
zju2132 The Most Frequent Number
Source
鸣谢 黄祎程
求众数 有个数一定会出现>n/2次 那么怎么办 我们就认为这个数是一个性质的数 另外其他的数看作其他的性质 那么我们如果有相同的就累加上 如果不相同就相当于把之前相同的都消去了 因为我这个数出现次数>n/2所以我无法用其他数完全消尽他 所以就okay了
#include<cstdio>
int n;
int main(){
freopen("bzoj2456.in","r",stdin);
scanf("%d",&n);int now=0,tot=0;scanf("%d",&now);tot=0;
for (int i=2;i<=n;++i){
int x;scanf("%d",&x);
if (x!=now) {
--tot;if (tot<0) now=x,tot=0;
}else ++tot;
}printf("%d",now);
return 0;
}