#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 ;
}
Problem Description
"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.

"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?

 
Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
 
Output
For each test case, you have to output only one line which contains the special number you have found.
 
Sample Input
5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1
 
Sample Output
3
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的更多相关文章

  1. HDU 1029 Ignatius and the Princess IV --- 水题

    HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...

  2. 怒刷DP之 HDU 1029

    Ignatius and the Princess IV Time Limit:1000MS     Memory Limit:32767KB     64bit IO Format:%I64d &a ...

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

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

  5. HDU 1029 一道微软面试题

    http://acm.hdu.edu.cn/showproblem.php?pid=1029 给定一个数组,其中有一个相同的数字是出现了大于等于(n + 1) / 2次的.要求找出来. 1.明显排序后 ...

  6. hdu 1029 求出现次数过半的数

    题目传送门//res tp hdu 已知必定存在一个元素出现次数超过一半,考虑用栈 若当前元素等于栈顶元素,入栈,反之出栈,并将当前元素入栈 最终的栈顶元素即是所求 #include<iostr ...

  7. hdu 1029 Ignatius ans the Princess IV

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  8. HDU 1029 Ignatius and the Princess IV

    解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...

  9. HDU 1029 Ignatius and the Princess IV DP

    kuangbin 专题 这题,有很多种解法. 第一种: 直接比较每个数出现次数. #include<iostream> #include<string> #include< ...

随机推荐

  1. opencv编程解决warning C4003: “max”宏的实参不足

    忘了把程序出错的代码附上了,运行修改好的程序才发现的.只好把问题的代码大致写一下了: warning C4003: “min”宏的实参不足 error C2589: “(”:“::”右边的非法标记 e ...

  2. 【最后一篇API译文】Android开发-API指南- Contacts Provider

    Contacts Provider 今年加入了某字幕组,加之杂事颇多,许久未添新文了,惭愧之极. 在听闻 Google 即将重返中国后,近日忽又发现官方网站正在放出 API 中文版,比如本文.当然不是 ...

  3. 如何用CSS进行开发

    翻译自:How to Develop with CSS 很多web开发人员都知道CSS,但是他们很可能认为使用CSS的应用代码就是这样的: <p> <span style=" ...

  4. OpenStack-Mitaka 一键安装测试环境脚本

    说明:这个脚本是采用Bash Shell编写,这个版本还只能作为测试环境搭建使用. 此脚本原形的发起人是网友:WuYuLiang.这里有他的博客链接:   第一版的链接: http://blog.cs ...

  5. 学习练习 java 输入输出流练习2

     编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt p ...

  6. openGL 提升渲染性能 之 顶点数组 VBO IBO VAO

    使用openGL图形库绘制,都需要通过openGL接口向图像显卡提交顶点数据,显卡根据提交的数据绘制出相应的图形. openGL绘制方式有:直接模式,显示列表,顶点数组,顶点索引. 直接模式:最简单, ...

  7. 解决问题 “You don't have permission to access /index.html on this server.”

    前几天装一个linux 企业版5.0安装了apache,打开测试页面的时候出现如下错误: Forbidden You don't have permission to access /index.ht ...

  8. Compass是什么

    Compass是一个强大的Sass框架,他的设计目标是顺畅.搞笑地装扮互联网,使用它的人可以写出可维护性更高的样式表. Compass由三个主要部分组成:混混合器和实用工具类库,能够集成到应用开发环境 ...

  9. CSS实用的代码段

    摘抄的一些代码还有自己总结的常用的代码~ 1>浏览器样式统一 *{ margin:0px; padding:0px; } 浏览器样式统一 2>清除浮动的方法 3>跨浏览器设置透明度 ...

  10. windows svn

    1.1Svn和VisualSvn介绍 VisualSvn Server2.5.6(版本控制服务器)免费开源软件 是基于Windows平台上的Subversion服务器,它是免费的 官方下载: http ...