You are given n points on a line with their coordinates xi. Find the point x 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 given n 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
原题链接:http://codeforces.com/contest/710/problem/B
题意:给你一些在X轴上的点,让你找一点,使其到其它所有点的距离之和最小。
好像和HDU11页上有一题好像。
AC代码:
#include <bits/stdc++.h>
using namespace std ;
const int maxn=int(3*1e5)+5;
int a[maxn];
int main()
{
int n;
ios::sync_with_stdio(false);
//freopen("B.txt","r",stdin);
while(cin>>n)
{
for(int i=0;i<n;i++)
cin>>a[i];
sort(a,a+n);
cout<<a[(n-1)/2]<<endl;
}
return 0;
}
尊重原创,转载请注明出处: http://blog.csdn.net/hurmishine

本文介绍了一个寻找线性坐标上使距离之和最小的点的问题及其解决方案。通过输入一系列整数坐标,采用排序方法找到中位数作为最优位置,确保了算法的高效性和正确性。

被折叠的 条评论
为什么被折叠?



