bzoj 2456: mode(找众数)

2456: mode

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

思路:对两两不相等的数进行抵消,则最后剩下的数则是该众数!  用num记录当前抵消后剩下的数,当num=0的时候把该数变为tmp,不等则cnt--,相等则cnt++  
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
    int tot = 1 , n , num , tem;
    scanf("%d%d",&n,&num);
    for(int i = 2 ; i <= n ; i++)
    {
        scanf("%d",&tem);
        if(tot == 0)
        {
            tot = 1;
            num = tem;
            continue;
        }
        if(num == tem)
            tot++;
        else
            tot--;
    }
    printf("%d\n",num);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值