给出数轴n个坐标,求一个点到所有点距离总和最小。排序后最中间一个点或两个点之间就是最优
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<stdio.h>
2 #include<algorithm>
3 using namespace std;
4 int x[502];
5
6 int main(){
7 int M;
8 while(scanf("%d",&M)!=EOF){
9 for (int q=1;q<=M;q++){
10 int N;
11 scanf("%d",&N);
12 for(int p=1;p<=N;p++){
13 scanf("%d",&x[p]);
14 }
15 sort(x+1,x+N+1);
16 int i,j,ans=0;
17 for(i=1,j=N;i<=j;i++,j--){
18 ans+=x[j]-x[i];
19 }
20 printf("%d\n",ans);
21 }
22 }
23 return 0;
24 }