HDU 1029 Ignatius and the Princess IV

HDU 1029 Ignatius and the Princess IV

HDU 1029 Ignatius and the Princess IV

题意:

出现超过n/2次的元素,是特殊的元素。

解题思路:

1. 在这个集合中每次删除两个不同的元素,那么剩下的最后一个元素,一定是特殊的元素。

2.把所有的元素排序,出现在n/2的元素,一定是特殊的元素

实现代码:

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 const int MAXN=10000000;
 6 
 7 int a[MAXN];
 8 
 9 int main(){
10     int n;
11     while(scanf("%d",&n)!=EOF){
12         for(int i=0;i<n;i++)
13             scanf("%d",&a[i]);
14         sort(a,a+n);
15         printf("%d
",a[n/2]);
16 
17     }
18 }