ZOJ - 2132:The Most Frequent Number(思维题)
pro:给定N个数的数组a[],其中一个数X的出现次数大于N/2,求X,空间很小。
sol:不能用保存数组,考虑其他做法。 由于出现次数较多,我们维护一个栈,栈中的数字相同,所以我们记录栈的元素和个数即可,如果新加入一个数与栈中数不同,则弹出一个元素(tot--),否则加入,最后保留在栈中的就是答案。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int D,tot,x,N;
int main()
{
while(~scanf("%d",&N)&&N){
tot=;
rep(i,,N) {
scanf("%d",&x);
if(tot==) D=x,tot=;
else if(D!=x) tot--;
else tot++;
}
printf("%d\n",D);
}
return ;
}
ZOJ - 2132:The Most Frequent Number(思维题)的更多相关文章
- ZOJ 2132 The Most Frequent Number (贪心)
题意:给定一个序列,里面有一个数字出现了超过 n / 2,问你是哪个数字,但是内存只有 1 M. 析:首先不能开数组,其实也是可以的了,后台数据没有那么大,每次申请内存就可以过了.正解应该是贪心,模拟 ...
- zoj2132-The Most Frequent Number
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2132 The Most Frequent Number Time Limi ...
- zoj 3778 Talented Chef(思维题)
题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...
- ZOJ 3829 贪心 思维题
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...
- ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)
ZOJ 2971 Give Me the Number 题目 ZOJ 2311 Inglish-Number Translator 题目 //两者题目差不多,细节有点点不一样,因为不是一起做的,所以处 ...
- C. Nice Garland Codeforces Round #535 (Div. 3) 思维题
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There Was One / POJ 3517 And Then There Was One / Aizu 1275 And Then There Was One (动态规划,思维题)
UVA 1394 And Then There Was One / Gym 101415A And Then There Was One / UVAlive 3882 And Then There W ...
- HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)
HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
随机推荐
- Google Adsense(谷歌网站联盟)广告申请指南
Google AdSense 是一种获取收入的快速简便的方法,适合于各种规模的网站发布商.它可以在网站的内容网页上展示相关性较高的 Google 广告,并且这些广告不会过分夸张醒目.由于所展示的广告同 ...
- Maven手动导本地jar到项目
第一步:先把目标jar安装在本地,下面是安装到本地的步骤:在cmd命令中,输入:mvn install:install-file -Dfile=C:\Users\Ter\Desktop\jqd_doc ...
- Ubuntu 定时关机
sudo shutdown +120 Shutdown scheduled for Sat 2019-09-21 23:48:44 CST, use 'shutdown -c' to cancel.
- redis的redisvCommand的%b
如下代码,向redis发送命令 SendCommand("HSET %b %b %b",key.data(),key.size(),filed.data(),filed.size( ...
- 如何理解 PHP的依赖注入(DI) 和 控制反转(IoC)
名词解释: IoC - Inversion of Control 控制反转 DI - Dependency Injection 依赖注入 依赖注入和控制反转说的实际上是同一个东西,它们是一种设计模式, ...
- vue2.0版本中v-html中过滤器的使用
Vue 2.0 不再支持在 v-html 中使用过滤器 解决方法: 1:全局方法(推荐) 2:computed 属性 3:$options.filters(推荐) 1:使用全局方法: 可以在 Vue ...
- stone2 [期望]
也许更好的阅读体验 \(\mathcal{Description}\) 有 \(n\) 堆石子,依次编号为 \(1, 2,\ldots , n\),其中第 \(i\) 堆有 \(a_i\) 颗石子 你 ...
- SpringbBoot之JPA批量更新
菜鸟学习,不对之处,还请纠正. 需要批量更新数据库的某些数据,项目使用的是JPA,刚对mybatis熟悉一点,又换成了JPA... 有点懵. 查询了一番之后,发现可以使用 In findByIdIn( ...
- np.newaxis的使用及有趣的数组相乘
a=np.array([1,2,3,4])a=a[np.newaxis,:] #固定行,相当于1行多列b=np.array([2,4,6]) b=b[:,np.newaxis] #固定列,相当与多行1 ...
- python 工厂方法
工厂方法模式(FACTORY METHOD)是一种常用创建型设计模式,此模式的核心精神是封装类中变化的部分,提取其中个性化善变的部分为独立类, 通过依赖注入以达到解耦.复用和方便后期维护拓展的目的. ...