您的位置:首页 > 其它

uva10041

2015-12-30 17:33 274 查看
题目大意:

求vito到各个邻居家的距离和要最小,最小为多少。

思路:

本来想说求平均数的,可惜WA了,后来想了个样例,1 2 2 2 2 2 ,

11/6 = 1的,其实要选择住在2距离才会最短。所以这题要求的是中位数。

代码:

#include <iostream>
using namespace std;
#include <cstring>
#include <stdio.h>
#include <cmath>
#include <algorithm>

int s[30002];

int main() {
int n,t;
int  sum;
scanf("%d",&n);
while(n--) {
sum = 0;
scanf("%d",&t);
for(int i = 0 ; i < t ; i++) {
scanf("%d",&s[i]);
}
int m;
sort(s,s+t);
if(!(t%2)) {
m = s[t / 2 - 1];
}
else
m = s[t / 2];
for(int i = 0; i < t; i++)
sum = sum + abs(s[i] - m);
printf("%d\n",sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: