BZOJ 2456 mode(卡内存)

2456: mode

Time Limit: 1 Sec   Memory Limit: 1 MB
Submit: 2697   Solved: 1145
[ 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。



zju2132 The Most Frequent Number




题解:先用map记录的,找中位数,卡内存啊。 一直改不出来,搜了题解,好巧妙的方法。


代码如下:


#include<cstdio>
int main()
{
	int n,cnt,num,a;
	while(scanf("%d",&n)!=EOF)
	{
		cnt=0,num=0x3f3f3f;
		while(n--)
		{
			scanf("%d",&a);
			if(a==num)
				cnt++;
			else
			{
				cnt--;
				if(cnt<=0)
				{
					cnt=1;
					num=a;
				}
			}
		}
		printf("%d\n",num);
	}
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值