您的位置:首页 > 其它

mode(BZOJ 2456)

2016-07-16 20:07 357 查看

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/2,所以应该设ans为众数,p为数量,每输入一个数,
若与众数相同,p++,否则与当前众数抵消。
*/
#include<cstdio>
using namespace std;
int p,ans,n;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
if(x==ans)p++;
else
{
p--;
if(p<=0)
{
ans=x;
p=1;
}
}
}
printf("%d",ans);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: