HDU 1029 Ignatius and the Princess IV (动态规划、思维)
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
"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
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
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 (动态规划、思维)的更多相关文章
- 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 --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- 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 Ignatius ans the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- [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 ...
- HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)
此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...
- HDU 1029 Ignatius and the Princess IV
解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...
- HDU 1029 Ignatius and the Princess IV DP
kuangbin 专题 这题,有很多种解法. 第一种: 直接比较每个数出现次数. #include<iostream> #include<string> #include< ...
- HDU 1029 Ignatius and the Princess IV(数论)
#include <bits/stdc++.h> using namespace std; int main(){ int n; while(~scanf("%d",& ...
随机推荐
- python的基础语法-部分Ⅱ
Python 保留字符 下面的列表显示了在Python中的保留字.这些保留字不能用作常数或变数,或任何其他标识符名称. 所有 Python 的关键字只包含小写字母. 行和缩进 学习 Python 与其 ...
- 如何查看 SQL Server 执行的历史 SQL 语句记录?
SELECT st.text as sql_statement, qs.creation_time as plan_last_compiled, qs.last_execution_time as p ...
- JavaScript 运算符是什么?
㈠JavaScript 运算符 ⑴运算符 = 用于赋值. ⑵运算符 + 用于加值. ⑶示例: 向变量赋值,并把它们相加: ; // 向 x 赋值 5 ; // 向 y 赋值 2 var z = ...
- java8 base64编码和解码
package com.oy; import java.nio.charset.StandardCharsets; import java.util.Base64; import org.junit. ...
- Visual Studio 2008:路径设置
造冰箱的大熊猫,本文适用于Visual Studio 2008中文版@cnblogs 2018/11/30 1.头文件路径设置 如果头文件所在路径未在环境变量中定义,编译时会出现C1083错误,提示无 ...
- POJ 2456 编程技巧之------二分查找思想的巧妙应用
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18599 Accepted: 8841 ...
- C_clock()测试代码(循环)运行时间
clock() 传送门(百度百科) clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t.在MSDN中,查得对clock函数定义如下: clock_t clock(voi ...
- MyBaits理解?
(1)MyBaits是一个基于Java的持久层框架,支持普通或定制化SQL查询.存储过程以及高级映射,使用XML或注解来配置和映射信息. (2)应用程序使用SQLSessionFactory实例,一个 ...
- C++入门经典-例3.7-用条件运算符判断数的奇偶性
1:条件运算符是一个三目运算符,能像判断语句一样完成判断.例如: max=(iA>iB) ? iA:iB; 意思是先判断iA是否大于iB,如果是,则max取iA的值,如果不是则取iB的值. 如果 ...
- Linux常用命令及操作
shutdown -r now现在重启 shutdown -h now现在关机 reboot重启 startx进入图形界面 chmod +x 777 文件名 授权和可执行 777为二进制111-111 ...