http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2132

The Most Frequent Number


Time Limit: 5 Seconds      Memory Limit: 1024 KB

Seven (actually six) problems may be somewhat few for a contest. But I am really unable to devise another problem related to Fantasy Game Series. So I make up an very easy problem as the closing problem for this contest.

Given a sequence of numbers A, for a number X if it has the most instances (elements of the same value as X) in A, then X is called one of the most frequent numbers of A. Now a sequence of numbers A of length L is given, and it is assumed that there is a number X which has more than L / 2 instances in A. Apparently X is the only one most frequent number of A. Could you find out X with a very limited memory?

Input

Input contains multiple test cases. Each test case there is one line, which starts with a number L (1 <= L <= 250000), followed by L numbers (-2^31 ~ 2^31-1). Adjacent numbers is separated by a blank space.

Output

There is one line for each test case, which is the only one most frequent number X.

Sample Input

5 2 1 2 3 2
8 3 3 4 4 4 4 3 4

Sample Output

2
4

思路:题意很简单,求一个数列里出现次数最多的那个数字,这个数字的个数是大于数列长度的1/2的。这里采用O(1)算法,利用大于1/2这个条件。

 #include <cstdio>

 int main()
{
int n,ans;
while(~scanf("%d",&n))
{
int m,cnt=;
for(int i=;i<n;i++)
{
scanf("%d",&m);
if(cnt==) ans=m,cnt++;
else if(ans==m) cnt++;
else cnt--;
}
printf("%d\n",ans);
}
return ;
}

zoj2132-The Most Frequent Number的更多相关文章

  1. ZOJ 2132 The Most Frequent Number (贪心)

    题意:给定一个序列,里面有一个数字出现了超过 n / 2,问你是哪个数字,但是内存只有 1 M. 析:首先不能开数组,其实也是可以的了,后台数据没有那么大,每次申请内存就可以过了.正解应该是贪心,模拟 ...

  2. ZOJ - 2132:The Most Frequent Number(思维题)

    pro:给定N个数的数组a[],其中一个数X的出现次数大于N/2,求X,空间很小. sol:不能用保存数组,考虑其他做法. 由于出现次数较多,我们维护一个栈,栈中的数字相同,所以我们记录栈的元素和个数 ...

  3. BZOJ 2456 杂题 卡内存

    2456: mode Time Limit: 1 Sec  Memory Limit: 1 MBSubmit: 3702  Solved: 1551[Submit][Status][Discuss] ...

  4. 【BZOJ2456】mode 神奇的卡内存题

    以后把题解放在前面,估计没人看题解先看题... 内存1M,4个int(其实对内存的概念十分模糊),众数即为出现次数最多的数,可以用抵消的思想(但是众数不是可以是一大坨么...) #include &l ...

  5. BZOJ 2456

    Description 给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数. Input 第1行一个正整数n.第2行n个正整数用空格隔开. Output 一行一个正整数表 ...

  6. Bzoj 2456: mode 数论,众数

    2456: mode Time Limit: 1 Sec  Memory Limit: 1 MBSubmit: 2843  Solved: 1202[Submit][Status][Discuss] ...

  7. 2456: mode

    2456: mode Time Limit: 1 Sec  Memory Limit: 1 MBSubmit: 4798  Solved: 2009[Submit][Status][Discuss] ...

  8. BZOJ 2456: mode(新生必做的水题)

    2456: mode Time Limit: 1 Sec  Memory Limit: 1 MB Submit: 4868  Solved: 2039 [Submit][Status][Discuss ...

  9. 【BZOJ】2456 mode(乱搞)

    Description 给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数. Input 第1行一个正整数n.第2行n个正整数用空格隔开. Output 一行一个正整数表 ...

随机推荐

  1. Corelocation及地图控件学习笔记

    Corelocation基本使用 在地图章节的学习中,首先要学的便是用户位置定位,因此我们首先要掌握Corelocation的使用.(在IOS8以前可以系统会直接请求授权,现在需要我们自己调用方式通知 ...

  2. andriod之摄像头驱动流程

    camera成像原理: 景物通过镜头生产光学图像投射到sensor表面上,然后转为模拟电信号,经过数模变成数字图像信号,在经过DSP加工出来,然后在通过IO接口传输到CPU处理. 由于摄像头满足总线. ...

  3. Java中去除StringBuffer最后一个字符

    原文:http://www.cnblogs.com/shaozhiheng/p/3661714.html 由于编写了这么一段代码: Iterator it3 = set.iterator(); whi ...

  4. shell中for循环

    shell中for循环总结 最常用的就是遍历操作某一类文件,比如批量建索引. for i in `ls` do samtools faidx $i done 注意:for末尾不需要冒号(:),循环的代 ...

  5. Dorado7检验器失效原因分析

    应用场景: AutoForm1使用包含A.B两个字段的DataType1. A字段不允许为空,并且B字段取值true时A字段需要重新输入,B取其它值时A值不需重新录入. 实现方法:A字段添加了非空检验 ...

  6. ActiveX控件(MFC篇)

    目录 第1章 VC++6.0创建控件    1 1.1 目标    1 1.1.1 方法    1 1.1.2 属性    1 1.1.3 事件    1 1.2 创建项目    2 1.3 项目结构 ...

  7. C# Socket编程(1)基本的术语和概念

    计算机程序能够相互联网,相互通讯,这使一切都成为可能,这也是当今互联网存在的基础.那么程序是如何通过网络相互通信的呢?这就是我记录这系列的笔记的原因.C#语言从一开始就是为了互联网而设计的,它为实现程 ...

  8. Git入门及上传项目到github中

    最近需要将课设代码上传到Github上,之前只是用来fork别人的代码. 这篇文章写得是windows下的使用方法. 第一步:创建Github新账户 第二步:新建仓库 第三部:填写名称,简介(可选), ...

  9. JAVA String作业——动手动脑以及课后实验性问题

    一:如何解释这样的输出结果?从中你能总结出什么?1.源码 //mao2015.10.20 //==的两个分辨 //对原始数据而言 //对引用类型变量 public class StringPool { ...

  10. hdu----(5053)the Sum of Cube(签到题,水体)

    the Sum of Cube Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...