[BZOJ2456]mode 其它

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2456

这道题有着神奇的内存限制1MB也就是说我们是没办法把读入的数字存下来的。

由于答案求的众数出现次数大于$\frac{n}{2}$,我们可以用一种抵消的思想,就是两个不相同的数互相抵消,这样最后剩下的那个数一定就是众数。

 1 #include<cstdio>
 2 int inline readint(){
 3     int Num;char ch;
 4     while((ch=getchar())<'0'||ch>'9');Num=ch-'0';
 5     while((ch=getchar())>='0'&&ch<='9') Num=Num*10+ch-'0';
 6     return Num;
 7 }
 8 int main(){
 9     int N=readint(),tmp,tot=0;
10     for(int i=1;i<=N;i++){
11         int x=readint();
12         if(x==tmp) tot++;
13         else if(x!=tmp){
14             if(!tot){
15                 tmp=x;
16                 tot=1;
17             }
18             else tot--;
19         }
20     }
21     printf("%d\n",tmp);
22     return 0;
23 }

 

posted @ 2017-09-28 20:28  halfrot  阅读(130)  评论(0编辑  收藏  举报