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",& ...
随机推荐
- Eclipse中SVN分支与合并
一.创建分支 Eclipse中利用svn插件创建分支,如下图 分支创建完毕,查看svn资源库中可以看到,分支信息: 二.分支合并主干 1.切换到分支,如图 切换: 2.修改分支信息 在pojo中新增T ...
- 【leetcode】1187. Make Array Strictly Increasing
题目如下: Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero ...
- [Flask]celery异步任务队列的使用
Celery异步任务队列 目录结构树: 配置文件config.py: # 设置中间人地址 broker_url = 'redis://127.0.0.1:6379/1' 主main.py: impor ...
- maven项目创建2
添加依赖索引 但是默认是没有索引的,要手动创建索引 依赖范围 debug 配置 运行常见问题 处理办法,JDK重新安装 网络添加依赖网站
- [笔记]MongoDB 二(Linux下MongoDB API C++编程)
一.连接类 DBClientConnection,派生自DBClientBase.DBClientBase类是实现query, update, insert, remove等功能. 构造函数:DBCl ...
- JQuery实现表格动态增加行并对新行添加事件
实现功能: 通常在编辑表格时表格的行数是不确定的,如果一次增加太多行可能导致页面内容太多,反应变慢:通过此程序实现表格动态增加行,一直保持最下面有多个空白行. 效果: 一:原始页面 二:表1增加新行并 ...
- 使用jQuery创建可删除添加行的动态表格,超级简单实用的方法
使用jQuery动态的添加和删除表格里面的行,不多说了直接上代码. <!DOCTYPE html> <html> <head> <meta charset=& ...
- Implement TensorFlow's next_batch for own data
The version of numpy data import numpy as np class Dataset: def __init__(self, data): self._index_in ...
- 剑指offer:关于复制构造函数
1:首先参看代码: #include "stdafx.h" #include "iostream" using namespace std; class A { ...
- gulp自动化构建工具安装使用(1)
我用的是windows,所以以下操作针对于windows用户,其他系统有不一样的地方请自行查阅资料更正. 好了,废话少说,反正也就是随手捣腾.下雨了,天晴了,我们开始搞gulp了 安装:gulp是个构 ...