http://acm.hdu.edu.cn/showproblem.php?pid=1029

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
 
时间复杂度:$O(N)$
代码:

#include <bits/stdc++.h>
using namespace std; const int maxn = 1e6 + 10;
int N;
int num[maxn], cnt[maxn]; int main() {
while(~scanf("%d", &N)) {
memset(num, 0, sizeof(num));
memset(cnt, 0, sizeof(cnt)); if(!N) break; for(int i = 1; i <= N; i ++) {
scanf("%d", &num[i]);
cnt[num[i]] ++;
} int temp;
for(int i = 1; i <= N; i ++) {
if(cnt[num[i]] >= (N + 1) / 2) {
temp = num[i];
}
}
printf("%d\n", temp);
}
return 0;
}

  

HDU B-Ignatius and the Princess IV的更多相关文章

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

    HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...

  2. 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 ...

  3. 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 ...

  4. hdu 1029 Ignatius ans the Princess IV

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  5. [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 ...

  6. HDU 1029 Ignatius and the Princess IV (动态规划、思维)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

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

    此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...

  8. HDU 1029 Ignatius and the Princess IV

    解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...

  9. HDU 1029 Ignatius and the Princess IV DP

    kuangbin 专题 这题,有很多种解法. 第一种: 直接比较每个数出现次数. #include<iostream> #include<string> #include< ...

  10. HDU 1029 Ignatius and the Princess IV(数论)

    #include <bits/stdc++.h> using namespace std; int main(){ int n; while(~scanf("%d",& ...

随机推荐

  1. flask笔记(三)Flask 添加登陆验证装饰器报错,及解析

    Flask 添加登陆验证装饰器报错,及解析 写这个之前,是想到一个需求,这个是关于之前写Flask笔记(二)中的一个知识点,路由相关 需求为 : 有一些页面必须是登陆之后才能访问的,比如Shoppin ...

  2. nodejs+express开发blog(2)

    npm install -g nodemon 1,把ejs文件修改为html文件 app.engine('.html', require('ejs').__express);app.set('view ...

  3. window.location.href url含中文乱码问题

    (1).页面中先对中文进行编码. 如:window.location.href = url+"&groupName=" + encodeURI(encodeURI(grou ...

  4. boost::shared_ptr文档翻译

    shared_ptr: 共享所有权 原文链接 描述 模版类 shared_ptr 存储动态构造对象的指针,通常是由C++ new语句完成的.这个对象指针在最后一个持有指针所有权的shared_ptr被 ...

  5. 【2018 ICPC亚洲区域赛沈阳站 L】Tree(思维+dfs)

    Problem Description Consider a un-rooted tree T which is not the biological significance of tree or ...

  6. Django从请求到返回流程

    图1:流程图 1. 用户通过浏览器请求一个页面2.请求到达Request Middlewares,中间件对request做一些预处理或者直接response请求3.URLConf通过urls.py文件 ...

  7. Redis4.0新特性

    redis 4.0 新特性 Redis 4.0在2017年7月发布为GA.包含几个重大改进:更好的复制(PSYNC2),线程DEL / FLUSH,混合RDB + AOF格式,活动内存碎片整理,内存使 ...

  8. git 本地分支与远程仓库分支关联

    当我们从远程仓库项目克隆到本地后,会自动创建本地master分支,并且与远程仓库主分支关联.如果我们需要在本地创建一个分支并且与远程仓库的origin/xxx分支关联,则可以通过以下命令实现 git ...

  9. nginx问题之nginx: could not build server_names_hash, you should increase server_names_hash_bucket_size解决方案

    昨天在nginx上部署了一个网站后,发现访问不了,再去访问之前部署的网站,发现都访问不了了,去看下下nginx,发现nginx服务停止了,没有在运行,重启了下服务,发现还是一样,就去看了下nginx的 ...

  10. jquery把数组中年月相同的数组重新组成新的数组

    //原数组var data = { results: [{ id:0, date:'2017-12-12', content:'123' },{ id:0, date:'2017-12-12', co ...