Ignatius and the Princess IV

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others)
Total Submission(s): 51503    Accepted Submission(s): 23178

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


Sample Output


题目大意与思路

输入一组数,n个,将该组数中相同数字的个数大于(n+1)/ 2 的数字输出。

我是看题目分类来做这个题的,怎么也想不出来动规怎么做 看了别人的题解恍然大悟 真是太高妙了

如果这个数出现次数大于(n+1)/ 2的话 那么他比所有其他的数出现次数都要多!

具体的看代码吧 应该很清楚了

#include<bits/stdc++.h>

using namespace std;

int i,n,cnt,anss,x;

int main()
{
while(scanf("%d",&n)!=EOF)
{
cnt=;
for(i=;i<=n;i++)
{
cin>>x;
if(cnt==)
{
cnt++;
anss=x;
}
else
{
if(x==anss)
cnt++;
else
cnt--;
}
}
cout<<anss<<endl;
}
}

HDU 1029 Ignatius and the Princess IV (动态规划、思维)的更多相关文章

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

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

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

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

  4. hdu 1029 Ignatius ans the Princess IV

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

  5. [ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)

    Ignatius and the Princess IV Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32767K (Ja ...

  6. HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)

    此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...

  7. HDU 1029 Ignatius and the Princess IV

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

  8. HDU 1029 Ignatius and the Princess IV DP

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

  9. HDU 1029 Ignatius and the Princess IV(数论)

    #include <bits/stdc++.h> using namespace std; int main(){ int n; while(~scanf("%d",& ...

随机推荐

  1. postman -- token全局变量

    var data = JSON.parse(responseBody);if (data.data.token) { tests["Body has token"] = true; ...

  2. 在qt的QOpenGLWidget开启opengl的抗锯齿

    在QOpenGLWidget的构造函数添加下面几句代码即可 QSurfaceFormat surfaceFormat; surfaceFormat.setSamples();//多重采样 setFor ...

  3. 51 Nod 不一样的猜字游戏

    1536 不一样的猜数游戏  题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 瓦斯亚和皮台亚在玩一个简单的游戏.瓦 ...

  4. scala实战学习-快速排序

    def qSort(a:List[Int]):List[Int]={ if(a.length < 2) a else qSort(a.filter(a.head > _)) ++ a.fi ...

  5. Java并发编程的艺术笔记(八)——线程池

    一.线程池的主要处理流程 ThreadPoolExecutor执行execute方法分下面4种情况. 1)如果当前运行的线程少于corePoolSize,则创建新线程来执行任务(注意,执行这一步需要获 ...

  6. bootstrap 学习笔记(部分)

    这个课程中的boostrap是3.0+版本的.(2.0与3.0有区别) bootstrap中的JS是依赖于jquery的,所以需要事先引用jquery(1.9.0版本以上). <!DOCTYPE ...

  7. typescript 函数(定义、参数、重载)

    代码: // 本节内容 // 1.函数的定义 // 2.参数(可选参数/默认参数/剩余参数) // 3.方法的重载 // js // function add(x,y){ // return x+y ...

  8. Git 合并两个分支内容

    1,将开发分支代码合入到master中 git checkout dev #切换到dev开发分支 git pull git checkout master git merge dev #合并dev分支 ...

  9. anaconda 安装pyspider出错

    注释Lib\mimetypes.py里面的 try:      mimetype = mimetype.encode(default_encoding) except UnicodeEncodeErr ...

  10. 如何保存 Activity 的状态?

    Activity 的状态通常情况下系统会自动保存的,只有当我们需要保存额外的数据时才需要使用到这样的功能.一般来说, 调用 onPause()和 onStop()方法后的 activity 实例仍然存 ...