杭电1029--Ignatius and the Princess IV(哈希)

Ignatius and the Princess IV

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32767K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 2

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

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

Ignatius.L

//哈希算法;

 1 #include<stdio.h>
 2 #include<string.h>
 3 int hash[1000000];
 4 int main()
 5 {
 6     int t;
 7     while(~scanf("%d",&t))
 8     {
 9         int i,temp;
10         memset(hash,0,sizeof(hash));
11         for(i=0;i<t;i++)
12         {
13             int a;
14             scanf("%d",&a);
15             hash[a]++;
16             if(hash[a]>=(t+1)/2)
17             temp=a;
18         }
19         printf("%d\n",temp);
20     }
21     return 0;
22 } 
时间: 2024-11-05 04:44:16

杭电1029--Ignatius and the Princess IV(哈希)的相关文章

杭电 1029 Ignatius and the Princess IV

http://acm.hdu.edu.cn/showproblem.php?pid=1029 Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K (Java/Others) Total Submission(s): 16754    Accepted Submission(s): 6730 Problem Description "OK, you ar

[2016-03-27][HDU][1029][Ignatius and the Princess IV]

时间:2016-03-30 22:03:01 星期三 题目编号:[2016-03-27][HDU][1029][Ignatius and the Princess IV] 题目大意:给定n个数字,输出其中出现次数超过n/2的数字 #include <algorithm> #include <cstdio> using namespace std; const int maxn = 1E6 + 10; int a[maxn]; int main(){ int n; while(~sc

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

HDU 1029 题目大意:给定数字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> #in

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

笨蛋的难题(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:1 描述        笨蛋之所以称为笨蛋,是因为他有点路痴.他一旦不高兴,就必然一个人漫无目的的出去走走.今天下雨了,他又不高兴了,怎么办?那就出去走呗,这不又丢了,这次幸好记下出来时的方向,并且在一张纸上密密麻麻的记下了他拐的弯(他很聪明吧,拐的弯都是90度的弯),0代表左拐,1代表右拐,那么多0.1,他实在看不下去了,正好遇见善良加聪明的你,你能告诉他,他现在面向哪吗? 输入 多组测试数据 第一行 输入:

HDU 1029: Ignatius and the Princess IV

Ignatius and the Princess IV ///@author Sycamore ///@date 8/8/2017 #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; while (cin >> n) { map<int, int>v; int t,ans=-1; for (int i = 1;

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

此题无法用JavaAC,不相信的可以去HD1029题试下! 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 hav

HDU 1029 Ignatius and the Princess IV DP

kuangbin 专题 这题,有很多种解法. 第一种: 直接比较每个数出现次数. 1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cstdio> 6 #include<set> 7 #include<map> 8 #include<vector> 9 #include<

hdu 1029 Ignatius and the Princess IV(排序)

题意:求出现次数>=(N+1)/2的数 思路:排序后,输出第(N+1)/2个数 #include<iostream> #include<stdio.h> #include<algorithm> using namespace std; int a[999999]; int main(){ int n,i; while(~scanf("%d",&n)){ for(i=0;i<n;++i) scanf("%d",&

Hdu oj 1029 Ignatius and the Princess IV

题目:点击打开链接 代码一: #include<stdio.h> #include<string.h> int b1[1000000]; int main() { int n; while(~scanf("%d",&n)) { int i; int a,t; memset(b1,0,sizeof(b1)); for(i=0;i<n;i++) { scanf("%d",&a); b1[a]++; if(b1[a]>=

杭电ACM1027——Ignatius and the Princess II

这题,找了好久,都没有找到是什么规律,百度了之后,才知道是第几个最小的排列是枚举排列的第几个. 真是长知识了!!~ 知道了这样的规律之后,就可以很快的写出了,algorithm这个头文件中又一个枚举排列的函数next_permutation,第i个最小的排列,就是调用next_permutation  i - 1次之后的那个排列.next_permutation同样的适用与可重集,也就是集合中存在重复元素. 下面是AC的代码: #include <iostream> #include <