首页 > 代码库 > BZOJ 2456 杂题 卡内存

BZOJ 2456 杂题 卡内存

2456: mode

Time Limit: 1 Sec  Memory Limit: 1 MB
Submit: 3702  Solved: 1551
[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

[Submit][Status][Discuss]

内存限制只有1M开了两个库就烂了

题目很水 求众数而且众数的个数大于n/2

所以就把不相同的两个数都消掉 一样的就累加起来 剩下的那个数就是众数

代码如下:

#include<cstdio>#define For(i,x,y) for(int i=x;i<=y;++i)using namespace std;int pre,pnum;int main(){    int n;scanf("%d",&n);int x;        For(i,1,n)    {        scanf("%d",&x);        if(pnum)        {            if(x==pre)pnum++;            else pnum--;        }        else        {            pre=x;            pnum=1;        }    }    printf("%d",pre);}

 

BZOJ 2456 杂题 卡内存