zoj2132-The Most Frequent Number
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的更多相关文章
- ZOJ 2132 The Most Frequent Number (贪心)
题意:给定一个序列,里面有一个数字出现了超过 n / 2,问你是哪个数字,但是内存只有 1 M. 析:首先不能开数组,其实也是可以的了,后台数据没有那么大,每次申请内存就可以过了.正解应该是贪心,模拟 ...
- ZOJ - 2132:The Most Frequent Number(思维题)
pro:给定N个数的数组a[],其中一个数X的出现次数大于N/2,求X,空间很小. sol:不能用保存数组,考虑其他做法. 由于出现次数较多,我们维护一个栈,栈中的数字相同,所以我们记录栈的元素和个数 ...
- BZOJ 2456 杂题 卡内存
2456: mode Time Limit: 1 Sec Memory Limit: 1 MBSubmit: 3702 Solved: 1551[Submit][Status][Discuss] ...
- 【BZOJ2456】mode 神奇的卡内存题
以后把题解放在前面,估计没人看题解先看题... 内存1M,4个int(其实对内存的概念十分模糊),众数即为出现次数最多的数,可以用抵消的思想(但是众数不是可以是一大坨么...) #include &l ...
- BZOJ 2456
Description 给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数. Input 第1行一个正整数n.第2行n个正整数用空格隔开. Output 一行一个正整数表 ...
- Bzoj 2456: mode 数论,众数
2456: mode Time Limit: 1 Sec Memory Limit: 1 MBSubmit: 2843 Solved: 1202[Submit][Status][Discuss] ...
- 2456: mode
2456: mode Time Limit: 1 Sec Memory Limit: 1 MBSubmit: 4798 Solved: 2009[Submit][Status][Discuss] ...
- BZOJ 2456: mode(新生必做的水题)
2456: mode Time Limit: 1 Sec Memory Limit: 1 MB Submit: 4868 Solved: 2039 [Submit][Status][Discuss ...
- 【BZOJ】2456 mode(乱搞)
Description 给你一个n个数的数列,其中某个数出现了超过n div 2次即众数,请你找出那个数. Input 第1行一个正整数n.第2行n个正整数用空格隔开. Output 一行一个正整数表 ...
随机推荐
- HDU5785 manacher+差分数组
用manacher算法O(n)求出所有的回文半径.有了回文半径后,就可以求出L[i]表示以i结尾的回文串的起始位置的和R[i]表示以i起始的回文串的结尾位置的和,然后就可以求出答案了,这里要注意奇偶长 ...
- Windows 内存架构
理解 Virtual Memory, Physical Memory, Committed Memory, Page File, Working Set, Modified Pages, Standb ...
- static final的理解
static: static静态,可以修饰类,成员变量,成员方法,代码块.static修饰的成员变量和方法独立于该类的任何对象,也就是被类的所有成员共享,这要这个类被加载,虚拟机就能根据类名在运行时数 ...
- graph-tool文档(一)- 快速开始使用Graph-tool - 1.创建和操纵图
目录: 快速开始使用graph-tool 创建和操纵图 -- 遍历顶点和边 ----- 遍历所有顶点或边 ----- 遍历一个顶点的neighbourhood 名词解释: instante:实例 di ...
- js实现页面跳转
js方式的页面跳转1.window.location.href方式 <script language="javascript" type="text/java ...
- 【转】 C++的深拷贝与浅拷贝
对于普通类型的对象来说,它们之间的复制是很简单的,例如:int a=88;int b=a; 而类对象与普通对象不同,类对象内部结构一般较为复杂,存在各种成员变量.下面看一个类对象拷贝的简单例子. ...
- js控制手机号码中间用星号代替
$("#tel").html($("#tel").substring(0,3)+"****"+$("#tel").sub ...
- Android系统启动过程-uBoot+Kernel+Android
摘要:本文是参考大量网上资源在结合自己查看源代码总结出来的,让自己同时也让大家加深对Android系统启动过程有一个更加深入的了解!再次强调,本文的大多数功劳应归功于那些原创者们,同时一些必要的参考链 ...
- robotframework笔记1
机器人框架是一个基于Python的,可扩展的关键字驱动的自动化测试最终到终端的验收测试和验收测试驱动开发(ATDD)框架.它可用于检测分布式异类应用程序,其中,验证要求感人几种技术和接口. 下面列出了 ...
- Ubuntu 安装 Redis
1. 下载并安装 redis 2.6.16版 sudo mkdir /usr/local/src/Redis cd /usr/local/src/Redis sudo wget http://down ...