bzoj2456: mode

2456: 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。


题解:
  这题其实很水,O(n)过...看代码理解吧
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define qread(x)x=read();
using namespace std;
inline int read()
{
    int f=1,x=0;char ch;
    while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return f*x;
}
int n,s,c;
int main()
{
    qread(n);c=0;
    while(n--)
    {
        int a;
        qread(a);
        if(s==a)c++;
        else
        {
            c--;
            if(c<=0)c=1,s=a;
        }
    }
    printf("%d\n",s);
    return 0;
}

 

 

posted @ 2017-11-26 14:59  CHerish_OI  阅读(161)  评论(0编辑  收藏  举报