BZOJ 2456: mode(乱搞)

挺神奇的一道题,被1M内存坑了好久= =,这道题得记录当前众数以及众数与其他数的差,如果现在读入的这个数与众数相等,就加1,否则减一,如果差为0就替代掉他,可以证明如果众数存在的话这样一定能找出来

CODE:

#include<cstdio>
using namespace std;
int n,x,sum,ans;
int main(){
 scanf("%d",&n);
 while (n--){
  scanf("%d",&x);
  if (sum==0) ans=x;
  if (x==ans) sum++;
  else sum--;
 }
 printf("%d",ans);
 return 0;
}

原文地址:https://www.cnblogs.com/New-Godess/p/4348941.html