题意:vito家有r个亲戚住在一条街道上,位置给出,要求输出从vito家到各个亲戚家总距离最短。
题解:如果要最短,就让vito家在距离最远的两个亲戚家的终点处,然后到各个亲戚家距离都加起来输出。
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 505;
int main() {
int t, r, s[N];
scanf("%d", &t);
while (t--) {
scanf("%d", &r);
for (int i = 0; i < r; i++)
scanf("%d", &s[i]);
sort(s, s + r);
int ans = 0;
for (int i = 0; i < r; i++)
ans += abs(s[i] - s[r / 2]);
printf("%d\n", ans);
}
return 0;
}