uva 10041

来源:互联网 发布:追梦设计美工学院 编辑:程序博客网 时间:2024/06/11 15:14

题意: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;}


0 0
原创粉丝点击