【BZOJ】2456: mode

2456: mode

Time Limit: 1 Sec  Memory Limit: 1 MB
Submit: 3154  Solved: 1342
[Submit][Status][Discuss]

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。


 

妈的,发现一道神题啊,这个空间表示我们只能开几个变量。但是这里的众数定义为出现次数大于n div 2.所以呵呵呵


 1 /**************************************************************
 2     Problem: 2456
 3     User: xrdog
 4     Language: C++
 5     Result: Accepted
 6     Time:192 ms
 7     Memory:820 kb
 8 ****************************************************************/
 9  
10 #include<cstdio>
11 using namespace std;
12 int i,x,tot,m,n;
13 inline int get()
14 {
15     int x=0,f=1;char ch=getchar();
16     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
17     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
18     return x*f;
19 }
20  
21 int main()
22 {
23     //freopen("a.in","r",stdin); freopen("a.out","w",stdout);
24     n=get();
25     x=-1;
26     for (i=1;i<=n;i++)
27         {
28             m=get();
29             if (m!=x) tot--; else tot++;
30             if (tot<1) {x=m; tot++;}
31         }
32     printf("%d",x);
33     return 0;
34 }

 

posted @ 2016-07-15 16:10  №〓→龙光←  阅读(145)  评论(0编辑  收藏  举报