hdu 1029
#include"stdio.h"
int main(void)
{
int n,x,y,t,i;
while(scanf("%d",&n)!=EOF)
{
scanf("%d",&x);
t=; for(i=;i<n;i++)
{
scanf("%d",&y);
if(y==x) t++;
else
if(t==) x=y;
else t--;
}
printf("%d\n",x);
}
return ;
}
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
5
1
其实最开始我选择用一个 500000 的数组,后来编译器玩不转我就放弃了,记得以前看到过一段最大子串和的代码,作者就用子串和与零的关系AC,觉得应该有戏,就朝着这方面分析。
问题的在于出现至少(N+1)/2 次的那个整数是多少?(注意(N+1)/2占据数列的一半多的数目)假设是 x ,对于该数列而言存在两种数:x,非x;x至少有(N+1)/2 个数,非x数少于有 (N+1)/2 个数,用H表示x的数目,Hh表示非x的数目,H-Hh>0;根据这个我们可以衍生这种想法:每一个x消耗一个非x,剩余的数肯定就是目标数。这一过程算法的实现就是代码7--17.
7-8是接收第一个数,作为比较的开始; 10--17就是在消耗非x数,如果新的到的数是x,t++,相反不是t--,这样就实现了对非x的消耗,就像这个数列只有1和2,如果得到1,我就t++,如果得到2,我就t--,如果t<=0了,那么我就让比较值为2,下一个数是2,t++,否则t--,相当于1和2在相互消耗,剩余的那个肯定就是数目最多的那个。
通过这个问题,我们应该思考算法最本质的东西,有很多种算法可以用,它们都是由前辈高手总结归纳或则研究出来的,对于不同的问题有着不同的策略,如果直接输出“Hello World”都要用算法解决,那就真的没意义了。对于不同的问题,会不会先尝试着建立数学模型,再想法转化为算法模型,(数学很重要呀),解决问题的思路各有不同,虽然可能代码相似,长期坚持,希望我能成为一个电脑高手!!
如果哪里有不对的地方希望大家能告诉我,向您学习。真心谢谢!
hdu 1029的更多相关文章
- HDU 1029 Ignatius and the Princess IV --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- 怒刷DP之 HDU 1029
Ignatius and the Princess IV Time Limit:1000MS Memory Limit:32767KB 64bit IO Format:%I64d &a ...
- 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 ...
- HDU 1029 Ignatius and the Princess IV (map的使用)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1029 Ignatius and the Princess IV Time Limit: 2000/10 ...
- HDU 1029 一道微软面试题
http://acm.hdu.edu.cn/showproblem.php?pid=1029 给定一个数组,其中有一个相同的数字是出现了大于等于(n + 1) / 2次的.要求找出来. 1.明显排序后 ...
- hdu 1029 求出现次数过半的数
题目传送门//res tp hdu 已知必定存在一个元素出现次数超过一半,考虑用栈 若当前元素等于栈顶元素,入栈,反之出栈,并将当前元素入栈 最终的栈顶元素即是所求 #include<iostr ...
- hdu 1029 Ignatius ans the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- HDU 1029 Ignatius and the Princess IV
解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...
- HDU 1029 Ignatius and the Princess IV DP
kuangbin 专题 这题,有很多种解法. 第一种: 直接比较每个数出现次数. #include<iostream> #include<string> #include< ...
随机推荐
- 飞达资讯App总体介绍及关系架构图
飞达资讯App总体介绍: 下图为飞达资讯App的关系架构图: 该App关系架构图所需的图片云盘链接地址:http://pan.baidu.com/s/1gfHIe4b 提取密码:x1nr 该App的云 ...
- 【测试】通过RMAN联机全库备份,包括控制文件,归档日志文件,备份成功后,删除已备份的归档日志。
RMAN是一个很方便很好用的备份,恢复,还原的一个工具,做这个小测试其实只有一个RMAN语句就完全解决了这么大的需求: RMAN> backup as backupset full databa ...
- hadoop fs管理文件权限
sudo addgroup Hadoop#添加一个hadoop组sudo usermod -a -G hadoop larry#将当前用户加入到hadoop组 修改hadoop目录的权限sudo ch ...
- 下雪了-js下雪效果
Jingle Bells,Jingle Bells,圣诞来临,做了一个下雪的小程序玩.有大雪花和小雪花. <!DOCTYPE html PUBliC "-//W3C//DTD XHTM ...
- linux设备分类
网络设备:常见的有以太网卡.CAN总线.WIFI.蓝牙 重要的结构体: net_device:用于描述网络设备的属性,为上层提供一个统一的操作接口.网络设备的驱动实际上就是填充此结构体,实现其中的各种 ...
- curl 取不到第二个参数解决方法
curl http://127.0.0.1:8083/search/add_index_quan_bc?option=1&channelId=139 在Linux下执行这个命令,观察日志发现, ...
- Linux系统快速启动方案
========================= 基本常识 ========================= Linux系统基本启动流程: 1. CPU从ROM(如果有的 ...
- JAVA之多线程的创建
转载请注明源出处:http://www.cnblogs.com/lighten/p/5967853.html 1.概念 老调重弹,学习线程的时候总会牵扯到进程的概念,会对二者做一个区分.网上有较多的解 ...
- 《深入剖析Tomcat》读书笔记(一)
一.Tomcat Tomcat,全名Apache Tomcat,最初是由Sun发起,后来捐赠给ASF,是Apache Jakarta下的一个子项目.Tomcat是对Servlet API定义的容器的一 ...
- MySQL语法
sql(structure query language)结构化查询语言ansi iso/iec组织制定ddl(data definition language) 数据定义语言dml(manipula ...