ZOJ 2132 The Most Frequent Number 贪心
来源:互联网 发布:济南网络服务公司 编辑:程序博客网 时间:2024/06/14 00:59
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 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.
<strong< dd="">
There is one line for each test case, which is the only one most frequent number X.
<strong< dd="">
5 2 1 2 3 28 3 3 4 4 4 4 3 4
<strong< dd="">
24
很经典的贪心题,最多的数比剩余的数多,所以直接贪心
#include <iostream>#include<cstdio>#include<cstring>using namespace std;int n;int main(){ while(~scanf("%d",&n)) { int t,a; int sum=0; for(int i=0;i<n;i++) { scanf("%d",&a); if(i==0) { sum++; t=a; } else { if(a==t) sum++; else { sum--; } if(sum<0) { t=a; sum=1; } } } printf("%d\n",t); }}
- ZOJ 2132 The Most Frequent Number 贪心
- zoj 2132 The Most Frequent Number
- ZOJ 2132 The Most Frequent Number
- ZOJ 2132 the most frequent number
- zoj 2132.The Most Frequent Number
- The Most Frequent Number
- BIT1006 The most frequent number
- BIT 1006 The most frequent number
- bit 1006 The most frequent number
- 寒假练习 1006 The most frequent number
- BZOJ2456 Mode & zju2132 The Most Frequent Number
- 2013寒假练习 1006:The most frequent number
- BIT寒假练习-2013__1006:The most frequent number
- BZOJ2456 Mode & zju2132 The Most Frequent Number(众数性质)
- checkio the most frequent weekdays
- Python checkio "The Most Frequent Weekdays"解决方案
- 反素数应用 ZOJ 2562 More Divisors + Timus OJ 1748. The Most Complex Number
- ZOJ-2132-The MostFrequent Number题解
- 富文本编辑器的使用
- Csharp中Thread的四种构造函数
- Python编程之文本编辑器
- SPSS MODELER支持的自动分类算法
- 棋盘覆盖问题
- ZOJ 2132 The Most Frequent Number 贪心
- 从对象深入分析 Java 中实例变量和类变量的区别
- 30天自制操作系统(第00天)
- 自学前端遇到瓶颈怎么办?
- matlab compiler & matlab coder
- 【POJ2718】Smallest Difference(枚举排列)
- handler内存泄露
- 从高考到程序员
- [Python爬虫]1.豆瓣电影Top250