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 一行一个正整数表 ...
随机推荐
- css3动画导航实现
代码 <!DOCTYPE html> <!-- saved from url=(0223)file:///C:/Users/user/AppData/Local/Temp/HZ$D. ...
- 【转载】C++异常机制的学习
参考了这篇文章:http://blog.chinaunix.net/uid-24517549-id-4079174.html 关于线程 进程和线程的概念相信各位看官早已耳熟能详.在这里,我只想带大家回 ...
- 企业信息化快速开发平台JeeSite
网站:http://jeesite.com/ 可用于企业后台管理
- [转]c++面向对象基础
from: here 1. C++面向对象程序设计的重要概念 早期革命影片里有这样一个角色,他说:“我是党代表,我代表党,我就是党.”后来他给同志们带来了灾难. 会用C++的程序员一定懂得面向对象程序 ...
- C#实现文件下载
1,Http 协议中有专门的指令来告知浏览器, 本次响应的是一个需要下载的文件. 格式如下:Content-Disposition: attachment;filename=filename.ext以 ...
- Vcenter 添加域管理员权限
授予相应管理权限
- 测序深度和覆盖度(Sequencing depth and coverage)
总是跑数据,却对数据一无所知,这说不过去吧. 看几篇文章吧 Sequencing depth and coverage: key considerations in genomic analyses( ...
- Sqoop安装配置及数据导入导出
前置条件 已经成功安装配置Hadoop和Mysql数据库服务器,如果将数据导入或从Hbase导出,还应该已经成功安装配置Hbase. 下载sqoop和Mysql的JDBC驱动 sqoop-1.2.0- ...
- Nodejs 配置+基础
Nodejs + NPP 配置. http://blog.csdn.net/foruok/article/details/48366765 NPM的全称是Node Package Manager,它就 ...
- Windows菜单
目录 第1章 Windows菜单 1 1.1 窗口菜单和弹出菜单 1 1.2 使用SetMenu 2 1.3 使用TrackPopupMenu 4 第1章 Windows菜单 ...