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(思维题)的更多相关文章

  1. ZOJ 2132 The Most Frequent Number (贪心)

    题意:给定一个序列,里面有一个数字出现了超过 n / 2,问你是哪个数字,但是内存只有 1 M. 析:首先不能开数组,其实也是可以的了,后台数据没有那么大,每次申请内存就可以过了.正解应该是贪心,模拟 ...

  2. zoj2132-The Most Frequent Number

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2132 The Most Frequent Number Time Limi ...

  3. zoj 3778 Talented Chef(思维题)

    题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...

  4. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  5. ZOJ 2971 Give Me the Number;ZOJ 2311 Inglish-Number Translator (字符处理,防空行,strstr)

    ZOJ 2971 Give Me the Number 题目 ZOJ 2311 Inglish-Number Translator 题目 //两者题目差不多,细节有点点不一样,因为不是一起做的,所以处 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 思维题--code forces round# 551 div.2

    思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...

随机推荐

  1. SpringBoot加载自定义yml文件

    自定义配置文件(跟SpringBoot的application.yml同一目录下): nlu-parse-rule: title: "NLU响应结果解析规则" desc: &quo ...

  2. centos 7 下使用jexus 配置ASP.NET Core

    1.安装jexus Jexus独立版(专业版)的安装 Jexus“独立版”指的是自带.net运行时(mono),不需要在客户服务器安装mono就能正常运行的Jexus版本,该版本只支持 64位Linu ...

  3. Instance Variable Hiding in Java

    class Test { // Instance variable or member variable private int value = 10; void method() { // This ...

  4. flask---快速使用

    初识falsk django是大而全面的框架,flask是个轻量级的框架. flask快速开发网站 flask可以使用很少的代码就可以直接完成一个项目(6,7行代码),如下: from flask i ...

  5. DRF框架(四)——单整体改(put)、单局部改(patch)、群局部改(patch)

    单整体改   单指的是单独一条数据,整体指这条数据的所有字段都必须传值修改 基于上篇文章的代码修改,序列化层不用变,只修改views.py 1) 单整体改,说明前台要提供修改的数据,那么数据就需要校验 ...

  6. Java8 集合相关操作

    // java8 集合快速转成string List<String> cities; String citiesCommaSeparated = String.join(",&q ...

  7. Python yield与实现(源码分析 转)

    转自:https://www.cnblogs.com/coder2012/p/4990834.html

  8. C#高效编程

    一. 使用readonly而不是const const是编译时常量,readonly是运行时常量.如果引用了一个库中的const常量,则在更新了程序集,但应用程序没有重新编译时,运行结果会出错 如程序 ...

  9. C# List<string>之间的转换

    List<string> 转换为 string List<string> list = new List<string>(); list.Add("a&q ...

  10. Java之路---Day09(继承)

    2019-10-23-22:58:23 目录 1.继承 2.区分成员变量重名的方法 3.区分成员方法重名的方法 4.继承中重写与重载的区别 5.继承中覆盖重写的注意事项 6.继承中覆盖重写的设计原则 ...