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. mysql 大数据量求平均值

    需求是:对于一个设备,求一天内每个小时的平均值,一个月内每天的平均值,更通用的需求是,从起始时间到结束时间,每隔一段时间,求一个平均值.目前的解决策略是:在存储过程中进行处理,从起始时间到结束时间,切 ...

  2. openerp安装记录及postgresql数据库问题解决

    ubuntu-14.04下openerp安装记录1.安装PostgreSQL 数据库    a.安装         sudo apt-get install postgresql    安装后ubu ...

  3. spring 好处与优点

    使用Spring有什么好处?(1)Spring能有效地组织你的中间层对象.(2)Spring能消除在许多工程中常见的对Singleton的过多使用.(3)Spring能消除各种各样自定义格式的属性文件 ...

  4. about opencl

    Platform:LG G3,Adreno 330 1. 8M(3264x2448) memmap方式读入时间24ms,读出时间12ms,时间与内存大小基本成线性关系.使用memmap 与 memco ...

  5. WAP调用微信支付https://pay.weixin.qq.com/wiki/doc/api/wap.php?chapter=15_1

    公司做的一个购物网站 之前微信版的网站要搬在webView上   可是微信支付是个问题 , 在外部浏览器怎么都发不起微信请求 , 原因是因为页面调用的微信浏览器自带JSAPI 在外部浏览器无法调用,但 ...

  6. 抓包工具Fidder设置(移动端抓包)

    1.下载安装fiddler,下载链接:http://fiddler2.com/get-fiddler(我用的是免安装的fiddler2) 2.设置fiddler 打开Fiddler,     Tool ...

  7. 【bzoj1027】合金

    [bzoj1027]合金 分析 数形结合+计算几何+Floyd最小环. http://blog.csdn.net/popoqqq/article/details/40539273 虽然这样占大家的很不 ...

  8. 如何通过ildasm/ilasm修改assembly的IL代码

    原文地址:http://kb.cnblogs.com/page/101162/ 这段时间为跟踪一个Bug而焦头烂额,最后发现是Framework的问题,这让人多少有些绝望.所以到微软论坛提了个帖子,希 ...

  9. html5重定义标签

    1.details: 可以同details与figure一同使用,定义包含文本 <details> <dd>无限互联1</dd><dd>无限互联1< ...

  10. 20145218 《Java程序设计》第7周学习总结

    20145218 <Java程序设计>第7周学习总结 教材学习内容总结 第十二章 Lambda 如果使用JDK8的话,可以使用Lambda特性去除重复的信息. 在只有Lambda表达式的情 ...