HDU - 1029 Ignatius and the Princess IV
题目链接
http://acm.split.hdu.edu.cn/showproblem.php?pid=1029
题目大意
给你n(n为奇数, n < 1e6)个数,让你找到个数 >=(n+1)/2 的那个数(保证存在)
思路
题目很简单,但是我们要找最好的实现方法
- 我过的方法有点炸胡,居然水过了, 开一个1e6的数组,统计每个数的出现次数,然后再扫一遍看哪个数出现次数 >= (n+1)/2,当时一直在找数字大小范围,题目没给,结果居然水过了,如果数字大一点这个办法就不行了
- 把所有数记在1e6的数组中,sort一遍,由于我们要找的那个数的个数 >=(n+1)/2 ,那么sort后中间那个数一定就是答案,这是最简便的写法,但还不是最优的
- 我们再次抓住个数 >= (n+1)/2 这个条件,我们把所有数扫一遍,把不同的两个数相互抵消,由于n是奇数,那么剩下的数一定就是答案,实现请看代码
该方法复杂的o(n),如果实现地细节一点,还可以省下1e6的空间
#include<bits/stdc++.h>
using namespace std;int main()
{int n;while(~scanf("%d", &n)){int tot = 0, ans = 0, tmp;//我们选定一个数ans作为基准for(int i = 0; i < n; ++i){scanf("%d", &tmp);if(tot == 0)//当基准数与其他数抵消完了{tot = 1;ans = tmp;//把基准换为当前这个数}else{if(tmp == ans) ++tot;else --tot;//两个数先不同,计数器--}}printf("%d\n", ans);}return 0;
}
HDU - 1029 Ignatius and the Princess IV相关推荐
- Hdu 1029 Ignatius and the Princess IV
思路:普通的cin会超时 方法很妙 1 #include<iostream> 2 #include<algorithm> 3 #include<string> 4 ...
- hdu-1029 Ignatius and the Princess IV
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1029 题目类型: 给一个奇数个列的数组,其中一定存在某个数字,该数字的个数是大于一半的,问这个数字是 ...
- E - Ignatius and the Princess IV
####E - Ignatius and the Princess IV "OK, you are not too bad, em- But you can never pass the n ...
- HDU 1026 Ignatius and the Princess I(BFS)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1026 在这博客学的 ttp://www.wutianqi.com/?p=2354 感觉看了这个之后收获 ...
- hdu 1398 Square Coins/hdu 1028 Ignatius and the Princess III
两道母函数的模板题: http://acm.hdu.edu.cn/showproblem.php?pid=1398 View Code #include<iostream>#include ...
- HDU1029 - Ignatius and the Princess IV【水题】
给你n个数字,请你找出出现至少(n+1)/2次的数字. 输入 本题包含多组数据,请处理到EOF: 每组数据包含两行. 第一行一个数字N(1<=N<=999999) ,保证N为奇数. 第二行 ...
- HDU 1026 Ignatius and the Princess I 迷宫范围内的搜索剪枝问题
这个问题是一个典型的类型的问题迷宫广泛的搜索. 在网上看到了很多解决方案. 没什么解决问题的分析报告,不指出其中的关键点.代码更像是一大抄.一些分析师也有很大的文章分析.只是不要全部命中关键,什么是广 ...
- hdu 1026 Ignatius and the Princess I(优先队列+bfs+记录路径)
以前写的题了,现在想整理一下,就挂出来了. 题意比较明确,给一张n*m的地图,从左上角(0, 0)走到右下角(n-1, m-1). 'X'为墙,'.'为路,数字为怪物.墙不能走,路花1s经过,怪物需要 ...
- HDU 1028 Ignatius and the Princess III
//强行递推. xx[i][j]表示i数中第j个开头的组合种类. /* 最终结果[i]为 sum of(xx[i][j]) (j from 1 to i); xx[i][j]=sum of (xx ...
最新文章
- 北斗卫星现在什么情况?这些数据不能不看
- bind1st bind2nd的使用
- 关于公司没有公网IP也没有动态IP,如何远程办公呢?
- 【网站】网站不能在QQ,微信中访问,申诉办法
- 扩展Reporting Services和SQL Server 2016中的更改
- Luogu P3373 【模板】线段树 2
- grafana默认用户名密码_提升运维格调?Grafana整合Zabbix
- 机器学习---算法---马尔科夫
- 关于DateTime的一个奇怪的现象
- Hibernate(转载)
- 数学建模方法-多项式拟合
- VS2015开发工具里面没有Arial字体怎么办?VS如何添加字体
- 电赛机器视觉——激光点定位
- python 函数注释出现 :expected an indented block
- PS2022安装步骤 ps 2022(详细安装方法)
- 【VOLTE】【高清语音】什么是高清语音
- linux 自带的python软连接被误删恢复
- MTK手机软件系统工程和配置简介
- 公司邮箱怎么注册?公司企业邮箱怎么登录?
- vscode底部蓝色导航不见了,vue less报错