[ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)
Ignatius and the Princess IV
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32767K (Java/Other)
Total Submission(s) : 7 Accepted Submission(s) : 3
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
N integers. The input is terminated by the end of file.
Output
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
Author
解题思路:
给出n个数。n为奇数。求在里面至少出现(n+1)/2 次的那个数。
方法一:
同等消除。比方数 -1 -2 3 3 3 -2 ,消除一次-1 3 -2 去掉。剩余3 3 -2 。再消除一次 剩余3,3就是所求。
代码:
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std; int main()
{
int n;
int num,cnt,r;
while(scanf("%d",&n)!=EOF)//一定得写上。EOF
{
cnt=0;
while(n--)
{
scanf("%d",&num);
if(cnt==0)
{
r=num;
cnt=1;
}
if(num==r)
cnt++;
else
cnt--; }
printf("%d\n",r);
}
return 0;
}
方法二:hash打表。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=1000005;
int hash[maxn]; int main()
{
int n,num,r;
while(scanf("%d",&n)!=EOF)
{
memset(hash,0,sizeof(hash));
for(int i=1;i<=n;i++)
{
scanf("%d",&num);
hash[num]++;
if(hash[num]>n/2)
{
r=num;//找到了也不能break掉。由于有可能还没有输入完成
}
}
printf("%d\n",r);
}
return 0;
}
[ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)的更多相关文章
- 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 / 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 ( ...
- HDU 1029 Ignatius and the Princess IV (动态规划、思维)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- 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",& ...
随机推荐
- iOS-Cocoapods更新不及时
一.问题 使用cocoapods搜索某些库时,搜索到的版本低于Github上面的版本,这样会导致一些问题.例如我在使用一个LTNavigationBar这个库时,在我升级到iOS10的时候,会导致导航 ...
- Python之数据结构:序列
一.序列 1.列表.元组和字符串都是序列 二.序列的两个特点:索引操作符和切片操作符 1.索引操作符:从序列中抓取一个特定项目 下标操作:使用索引获取序列中的单个项目: eg:shoplist[0] ...
- bzoj 1367 - sequence
Description 给定一个序列\(t_1,t_2,\cdots,t_n\),求一个递增序列\(z_1<z_2<...<z_n\), 使得 \(R=|t_1−z_1|+|t_2− ...
- 挑战程序设计竞赛》P345 观看计划
<挑战程序设计竞赛>P345 观看计划 题意:一周一共有M个单位的时间.一共有N部动画在每周si时 ...
- Python之Django框架
1.Django简介 Python下有多款不同的 Web 框架,Django是最有代表性的一种.许多成功的网站和APP都基于Django. Django是一个开源的Web应用框架,由Python写成. ...
- Ant自动打包
在ant的官网http://ant.apache.org进行下载后apache-ant-1.8.2包 解压(存放的路径不要有中文字符) 把ant里的lib设置到环境变量:E:\Android\apac ...
- MSClass (Class Of Marquee Scroll通用不间断滚动JS封装类) Ver 1.65
原文发布时间为:2010-02-07 -- 来源于本人的百度文章 [由搬家工具导入] http://www.popub.net/script/MSClass.html/*MSClass (Class ...
- [SaltStack] Minion-conf自动更新
minion-conf配置文件自动更新, 加载 minion-conf是每个minion自身以来的配置, 为了方便我们在中心管控机上(Master)统一配置, 然后下发文件, 进而使得Minion能够 ...
- WSL使用小结:从ArchLinux到Manjaro
1.前言 上一篇介绍了Windows 10下配置WSL环境,通过ALWSL脚本替换为ArchLinux的过程.这一篇介绍根据ArchLinux官网的说明,在WSL下安装ArchLinux,并切换到发行 ...
- 定时执行rsync同步数据以及mysql备份
需求:把机器A中的附件.图片等,备份到备份机B中.将数据库进行备份 附件备份 在A中,启动rsync服务,编辑/etc/xinetd.d/rsync文件,将其中的disable=yes改为disabl ...