hdu 1029 Ignatius and the Princess IV
Ignatius and the Princess IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K (Java/Others)
Total Submission(s): 13455 Accepted Submission(s): 5435
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
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
Recommend
/*
题意:
给出n个数,保证有一个数最少出现(n+1)/2次,求该数
类似于求众数
有好几种做法:
1、hash
2、排序后的中位数
3、加减的不知道叫什么方法
*/
题意:
给出n个数,保证有一个数最少出现(n+1)/2次,求该数
类似于求众数
有好几种做法:
1、hash
2、排序后的中位数
3、加减的不知道叫什么方法
*/
方法1:
1 //250MS 4120K 371 B G++ 2 /* 3 这种方法要求出现的数要小于1000000 4 */ 5 #include<stdio.h> 6 #include<string.h> 7 int a[1000000]; 8 int main(void) 9 { 10 int n,a0; 11 while(scanf("%d",&n)!=EOF) 12 { 13 memset(a,0,sizeof(a)); 14 int maxn=0; 15 for(int i=0;i<n;i++){ 16 scanf("%d",&a0); 17 a[a0]++; 18 if(a[a0]>a[maxn]) maxn=a0; 19 } 20 printf("%d\n",maxn); 21 } 22 return 0; 23 }
方法2:
1 //296MS 1384K 363 B G++ 2 #include<stdio.h> 3 #include<stdlib.h> 4 int a[1000000]; 5 int cmp(const void*a,const void*b) 6 { 7 return *(int*)a-*(int*)b; 8 } 9 int main(void) 10 { 11 int n; 12 while(scanf("%d",&n)!=EOF) 13 { 14 for(int i=0;i<n;i++) 15 scanf("%d",&a[i]); 16 qsort(a,n,sizeof(a[0]),cmp); 17 printf("%d\n",a[(n+1)/2]); 18 } 19 return 0; 20 }
方法3:
1 //234MS 1380K 424 B G++ 2 #include<stdio.h> 3 int a[1000000]; 4 int main(void) 5 { 6 int n; 7 while(scanf("%d",&n)!=EOF) 8 { 9 for(int i=0;i<n;i++) 10 scanf("%d",&a[i]); 11 int maxn=0; 12 int count=0; 13 for(int i=0;i<n;i++){ 14 if(count==0) maxn=a[i]; 15 if(a[i]==maxn) count++; 16 else count--; 17 } 18 printf("%d\n",maxn); 19 20 } 21 return 0; 22 }