HDU 2083 简易版之最短距离
来源:互联网 发布:数据分析 excel 编辑:程序博客网 时间:2024/05/29 14:02
Problem Description
寒假的时候,ACBOY要去拜访很多朋友,恰巧他所有朋友的家都处在坐标平面的X轴上。ACBOY可以任意选择一个朋友的家开始访问,但是每次访问后他都必须回到出发点,然后才能去访问下一个朋友。
比如有4个朋友,对应的X轴坐标分别为1, 2, 3, 4。当ACBOY选择坐标为2的点做为出发点时,则他最终需要的时间为 |1-2|+|2-2|+|3-2|+|4-2| = 4。
现在给出N个朋友的坐标,那么ACBOY应该怎么走才会花费时间最少呢?
Input
输入首先是一个正整数M,表示M个测试实例。每个实例的输入有2行,首先是一个正整数N(N <= 500),表示有N个朋友,下一行是N个正整数,表示具体的坐标(所有数据均<=10000).
Output
对于每一个测试实例,请输出访问完所有朋友所花的最少时间,每个实例的输出占一行。
Sample Input
2
2
2 4
3
2 4 6
Sample Output
2
4
解析
题目要求是“ACBOY可以任意选择一个朋友的家开始访问”,提前对数据排下序,然后只要找到n/2个数据点,在该点作为出发点的结果就是最小。
代码
#include <iostream>#include <stdlib.h>#include <math.h>#include<algorithm>using namespace std;int cmp(int a, int b){ return a<b;}int main(){ int a[505]; int M; cin>>M; while(M--) { int n; cin>>n; int min=0; int temp = n/2; int i,j; for(i = 0; i < n; i++) { cin>>a[i]; } sort(a, a+n, cmp); for(i = 0; i < n; i++) { min += abs(a[temp]-a[i]); } cout<<min<<endl; } return 0;}
0 0
- HDU 2083 简易版之最短距离
- hdu 2083 简易版之最短距离
- HDU 2083 简易版之最短距离
- HDU 2083简易版之最短距离
- HDU 2083 简易版之最短距离
- HDU 2083 简易版之最短距离
- HDU 2083 简易版之最短距离
- hdu 2083 简易版之最短距离
- hdu 2083 简易版之最短距离
- HDU 2083 简易版之最短距离
- 【HDU】2083简易版之最短距离
- HDU 2083 简易版之最短距离
- HDU 2083 简易版之最短距离
- HDU - 2083 - 简易版之最短距离
- HDU 2083简易版之最短距离
- HDU 2083 简易版之最短距离
- hdu 2083 简易版之最短距离
- hdu 2083 简易版之最短距离
- home-ids-with-snort-and-snorby
- 伪元素和伪类的区别
- matlab下生成的矩阵存入xml文件中
- poj3335 Rotating Scoreboard【半平面交】
- MI(mutal information)and Entropy
- HDU 2083 简易版之最短距离
- ubuntu16.04 安装 Eric6
- JZOJ1332. 正方形内的计数
- uva 11077 Find the Permutations 置换+递推
- spark-streaming-[2]-累加器(更新器)操作(updateStateByKey)
- C#编程实现获取当前计算机的名字
- POJ 2553
- poj1474 Video Surveillance【半平面交】
- 步循环