HDOJ 1029 Ignatius and the Princess IV (水题)
来源:互联网 发布:遭遇网络敲诈怎么办 编辑:程序博客网 时间:2024/04/30 22:55
Ignatius and the Princess IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K (Java/Others)Total Submission(s): 23299 Accepted Submission(s): 9760
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?
"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
51 3 2 3 3111 1 1 1 1 5 5 5 5 5 571 1 1 1 1 1 1
Sample Output
351
题意:n个数,找出数量>=(n+1)/2的数
思路:sort后查找就行了
ac代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 1010000#define LL long long#define ll __int64#define INF 0xfffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)using namespace std;int a[MAXN];int main(){int t,n,m,i,j,k;int cas=0;while(scanf("%d",&n)!=EOF){for(i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n);int k=a[0];int cnt=(n+1)/2;int num=1;int bz=0;for(i=1;i<n;i++){if(a[i]==k)num++;else{if(num>=cnt){bz=1;printf("%d\n",k);break;}k=a[i];num=1;}}if(bz==0){if(num>=cnt)printf("%d\n",k);}}return 0;}
0 0
- 【HDOJ】1029 -> Ignatius and the Princess IV
- hdoj 1029 Ignatius and the Princess IV
- HDOJ 1029 Ignatius and the Princess IV
- HDOJ Ignatius and the Princess IV 1029
- HDOJ 1029 Ignatius and the Princess IV
- hdoj 1029 Ignatius and the Princess IV
- HDOJ 1029 Ignatius and the Princess IV
- hdoj-1029-Ignatius and the Princess IV
- hdoj 1029 Ignatius and the Princess IV
- HDOJ 1029 Ignatius and the Princess IV
- HDOJ 1029 Ignatius and the Princess IV (水题)
- HDOJ 1029 Ignatius and the Princess IV [map]
- HDOJ 1029:Ignatius and the Princess IV 解题报告
- hdoj.1029 Ignatius and the Princess IV 20140809
- HDOJ 1029 Ignatius and the Princess IV 【math】
- hdoj 1029 Ignatius and the Princess IV 【简单】
- HDOJ 1029 Ignatius and the Princess IV(易题)
- 杭电HDOJ 1029 Ignatius and the Princess IV
- Efficient Route Planning on Public Transportation Networks: A Labelling Approach
- 数组形参
- 2015年10月19日
- 文件隐藏属性chattr,lsattr
- main处理命令行选项
- HDOJ 1029 Ignatius and the Princess IV (水题)
- 基本类型与引用类型区别
- 2015年10月20日
- Java线上应用故障排查之一:高CPU占用
- 脚本文件名的查询和文件名的查找
- 归并排序的思路
- 强连通分量scc
- 你为什么不快乐
- JDK,JRE,JVM区别与联系