排序+二分,分别找到中间数的最左边的位置和最右边的位置
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int q[N],n;
int find_left(int x){
int l = 0,r = n - 1;
while(l < r){
int mid = l + r >> 1;
if(q[mid] >= x) r = mid;
else l = mid + 1;
}
return l;
}
int find_right(int x){
int l = 0,r = n - 1;
while(l < r){
int mid = l + r + 1 >> 1;
if(q[mid] <= x) l = mid;
else r = mid - 1;
}
return l;
}
int main(){
cin >> n;
for(int i = 0;i < n;i++) cin >> q[i];
sort(q,q + n);
int mid = q[(n - 1) / 2];
int l = find_left(mid),r = find_right(mid);
if(l == n - 1 - r) cout << mid << endl;
else cout << -1 << endl;
return 0;
}