You are given n points on a line with their coordinatesxi. Find the pointx so the sum of distances to the given points is minimal.
The first line contains integer n (1 ≤ n ≤ 3·105) — the number of points on the line.
The second line contains n integers xi ( - 109 ≤ xi ≤ 109) — the coordinates of the givenn points.
Print the only integer x — the position of the optimal point on the line. If there are several optimal points print the position of the leftmost one. It is guaranteed that the answer is always the integer.
4 1 2 3 4
2
解:在一条线上给定一组坐标,找到一点到其余各点之间的距离最小。对坐标进行排序一下,中间的坐标则到各个坐标的距离最小。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int a[300005];
int main()
{
long long n;
while(~scanf("%I64d",&n)){
memset(a,0,sizeof(a));
for(long long i=0;i<n;i++){
scanf("%d",&a[i]);
}
sort(a,a+n);
printf("%d\n",a[(n-1)/2]);
}
return 0;
}