HDU 1029 Ignatius and the Princess IV --- 水题
题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数
解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出现的次数,
若次数一旦达到(n+1)/2,即输出a[i]
注意能出现(n+1)/2次数的最多只有一个
/* HDU 1029 *Ignatius and the Princess IV --- dp */
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std; const int maxn = ;
int a[maxn]; map<int, int> m; int main()
{
#ifdef _LOCAL
freopen("D:\\input.txt", "r", stdin);
#endif int n;
int ans;
while (scanf("%d", &n) == ){
m.clear(); //记得清空原有的东西
for (int i = ; i <= n; ++i){
scanf("%d", a + i);
++m[a[i]];
if (m[a[i]] == (n + ) / ){
ans = a[i];
}
}//for(i)
printf("%d\n", ans);
} return ;
}
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 (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 ...
- HDU 1029 Ignatius and the Princess IV (动态规划、思维)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- HDU 1029 Ignatius and the Princess IV
解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...
- HDU1029 Ignatius and the Princess IV (水题)
<题目链接> 题目大意:给你一段序列,问你在这个序列中出现次数至少为 (n+1)/2 的数是哪个. 解题分析: 本题是一道水题,如果用map来做的话,就非常简单,但是另一个做法还比较巧妙. ...
- 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 DP
kuangbin 专题 这题,有很多种解法. 第一种: 直接比较每个数出现次数. #include<iostream> #include<string> #include< ...
随机推荐
- Chart For Asp.Net ----Overview
一个图表有很多元素构成,所有元素都能通过图表API控制.图表API是面向对象的,可扩展的,高复用的.支持很多图表元素如:data series,data points in a series,char ...
- 经典线程同步 事件Event
阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇 一个经典的多线程同步问题> <秒杀多线程第五篇 经典线程同步关键段CS> 上一篇中使用关键段来解决经典的多线程同步互斥问题 ...
- Oracle中varchar,varchar2,nvarchar,nvarchar2的区别
--varchar,varchar2 联系:1.varchar/varchar2用于存储可变长度的字符串比如varchar(20),存入字符串'abc',则数据库中该字段只占3个字节,而不是20个字节 ...
- iOS网页开发技术总结
网页组成 一个有具体功能的完整的网页,一般由3部分组成 HTML:网页的具体内容和结构 CSS:网页的样式(美化网页最重要的一块) JavaScript:网页的交互效果,比如对用户鼠标事件做出响应 H ...
- YAML 语言语法
发现很多开源的软件的配置文件都使用了这种语言来描述,据说是简单强大,很不巧,ansible也使用了这种语言来描述配置,学习ansible之前,先学习一下YAML语言. YAML基本语法规则如下: 大小 ...
- hdu4283 区间dp
//Accepted 300 KB 0 ms //区间dp //dp[i][j] 表示i到j第一个出场的最小diaosizhi //对于i到j考虑元素i //(1)i第一个出场,diaosizhi为 ...
- json_decode 与 json_encode 的区别
1.json_decode对JSON格式的字符串进行编码 2.json_encode对变量进行 JSON 编码 3.unset()是注销定义的变量 4.urlencode()函数原理就是首先把中文字符 ...
- pyMagic:用python控制的Geek入门神器
大学四年快要结束了,这几年也学习了一点新的姿势.最近一直在跟国外的micropython项目,这个项目是由剑桥大学的理论物理学家(theoretical physicist)Damien George ...
- UIkit框架之UIimageview
1.继承链:UIview:UIresponder:NSObject 2.如果你想利用这个类来制作动态图片,你需要遵守以下的原则: (1)所有的图片的大小都要一样 (2)所有的图片要使用同样的比例,同样 ...
- Interview----判断整数序列是否是二叉搜索树的后序遍历结果
题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果. 如果是返回true,否则返回false. 例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果: ...