#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. 001 The Hello World In Csharp

    C#是面向对象编程语言,语法和JAVA非常相似.接下来让我们看一下C#的Hello world. //001.cs using System; public class Hello { public ...

  2. 网站图片优化-解码JPEG

    首先,老大拿了两个网站工具的分析跟我说,让我分析一下我们网站的图片有没有什么方法优化. [网站分析工具]webpage test: http://www.webpagetest.org/谷歌pages ...

  3. 小巧的http live streaming m3u8播放器

    转载请注明: TheViper http://www.cnblogs.com/TheViper  原来发表过一篇分段播放的flash播放器.这个播放器其实就没有神马原理,就是把一个视频分成好几个视频, ...

  4. infoWindowRenderer之个人见解

    在地图上以类似于提示框的方式显示查询信息,类似于arcmap中单击图层查看属性 由于Class Graphic有infoWindowRenderer : IFactory这个属性 (注:infoWin ...

  5. sendmessage和postmessage的区别

    BOOL   PostMessage(          HWND   hWnd,             //   handle   of   destination   window        ...

  6. RC振荡器与有源滤波器1

    自我总结,有错误欢迎指出! 一.振荡器原理: 对于正反馈,有如下公式: 讨论: 1.当A*F(jw)=1时 Af→∞(物理意义:自激振荡): 2.在f0上,F(jw)相移为0或2n∏:(因此,在f0处 ...

  7. ICDM 2007

    Language-Independent Set Expansion of Named Entities Using the Web. Chao Wang, Venu Satuluri, Sriniv ...

  8. jQuery Mask

    <script type="text/javascript" src="/assets/mask/jquery.mask.min.js"></ ...

  9. 开源项目:网页实时通信WebRTC

    参考资料 [博客系列] Android WebRTC 音视频开发总结 [环境编译] WebRTC入门指南 各平台WebRTC源码网盘下载 Ubuntu14.04编译WebRTC For Android ...

  10. 轻松找回Win7桌面“消失”的IE9图标

    打开注册表编辑器(Win+R打开运行窗口,运行regedit命令),依次展开到 [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion ...