hdu4311 曼哈顿距离
来源:互联网 发布:visio网络拓扑图素材 编辑:程序博客网 时间:2024/05/17 23:10
http://acm.hdu.edu.cn/showproblem.php?pid=4311
Problem Description
It has been ten years since TJU-ACM established. And in this year all the retired TJU-ACMers want to get together to celebrate the tenth anniversary. Because the retired TJU-ACMers may live in different places around the world, it may be hard to find out where to celebrate this meeting in order to minimize the sum travel time of all the retired TJU-ACMers.
There is an infinite integer grid at which N retired TJU-ACMers have their houses on. They decide to unite at a common meeting place, which is someone's house. From any given cell, only 4 adjacent cells are reachable in 1 unit of time.
Eg: (x,y) can be reached from (x-1,y), (x+1,y), (x, y-1), (x, y+1).
Finding a common meeting place which minimizes the sum of the travel time of all the retired TJU-ACMers.
There is an infinite integer grid at which N retired TJU-ACMers have their houses on. They decide to unite at a common meeting place, which is someone's house. From any given cell, only 4 adjacent cells are reachable in 1 unit of time.
Eg: (x,y) can be reached from (x-1,y), (x+1,y), (x, y-1), (x, y+1).
Finding a common meeting place which minimizes the sum of the travel time of all the retired TJU-ACMers.
Input
The first line is an integer T represents there are T test cases. (0<T <=10)
For each test case, the first line is an integer n represents there are n retired TJU-ACMers. (0<n<=100000), the following n lines each contains two integers x, y coordinate of the i-th TJU-ACMer. (-10^9 <= x,y <= 10^9)
For each test case, the first line is an integer n represents there are n retired TJU-ACMers. (0<n<=100000), the following n lines each contains two integers x, y coordinate of the i-th TJU-ACMer. (-10^9 <= x,y <= 10^9)
Output
For each test case, output the minimal sum of travel times.
Sample Input
46-4 -1-1 -22 -40 20 35 -260 02 0-5 -22 -2-1 24 05-5 1-1 33 13 -11 -110-1 -1-3 2-4 45 25 -43 -14 3-1 -23 4-2 2
Sample Output
26202056HintIn the first case, the meeting point is (-1,-2); the second is (0,0), the third is (3,1) and the last is (-2,2)
/**hdu 4311 曼哈顿距离 题目大意:在给定的n个点中选择一个点,使得其他点到这个点的曼哈顿距离之和最小,求出这个最小的距离解题思路:如果我们确定了这个点的坐标为 (x,y).xx为所有点的横坐标之和,numlx表示该点左边的点的个数, 那么lengx=(x*numlx-sumx[1~numlx-1])+(sumx[numlx~n]-x*(n-numlx))=x*(2*numlx-n)+xx-2*sum[1~numlx]; 对于纵坐标的处理类似。 这些工作做好之后我们把n个点都枚举1遍取最小就可以了。*/#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;typedef long long LL;const int maxn=100005;struct note{ int x,y,id;} a[maxn];int x[maxn],y[maxn],n;LL numx[maxn],numy[maxn];bool cmp1(note a,note b){ return a.x<b.x;}bool cmp2(note a,note b){ return a.y<b.y;}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d",&n); LL sumx=0,sumy=0; for(int i=1; i<=n; i++) { scanf("%d%d",&x[i],&y[i]); a[i].x=x[i]; a[i].y=y[i]; a[i].id=i; sumx+=x[i]; sumy+=y[i]; } sort(a+1,a+n+1,cmp1); LL ans=0; for(int i=1; i<=n; i++) { ans+=a[i].x; int j=a[i].id; numx[j]=(LL)x[j]*(2*i-n)+sumx-2*ans; } sort(a+1,a+1+n,cmp2); ans=0; for(int i=1; i<=n; i++) { ans+=a[i].y; int j=a[i].id; numy[j]=(LL)y[j]*(2*i-n)+sumy-2*ans; } ans=numy[1]+numx[1]; for(int k=2; k<=n; k++) ans=min(ans,numx[k]+numy[k]); printf("%I64d\n",ans); } return 0;}
0 0
- hdu4311 曼哈顿距离
- HDU4311 Meeting point-1(曼哈顿距离)
- 某个点到其他点的曼哈顿距离之和最小(HDU4311)
- 曼哈顿距离
- 曼哈顿距离
- 曼哈顿距离
- 曼哈顿距离
- 曼哈顿距离
- hdu4311
- hdu4311
- 最远(近)曼哈顿距离
- 模版-最长曼哈顿距离
- ]Requirements[最远曼哈顿距离]
- 最远曼哈顿距离
- BZOJ3210【曼哈顿距离】
- zoj2688 Requirements 曼哈顿距离
- poj2926Requirements (曼哈顿距离)
- 2015蓝桥杯 曼哈顿距离
- malloc和free函数详解
- 不能完成此操作, 因为发生意外错误(错误代码-51)
- 使用Ant和org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader打包可运行Jar后找不到main的解决方案
- 排序算法之归并排序
- 萌新自己瞎写的2048
- hdu4311 曼哈顿距离
- SQL2008 打开问题——评估期已过
- 判断回文数
- c++中while和for的优缺点
- 14382Life is beautiful
- 不带头节点的头尾插法
- chrome运行webdriver脚本提示--ignore-certificate-errors
- 关于WIN7+VS2013+WDK8.1做出一个.sys文件
- 几个程序解惑java多线程