首页 > 代码库 > hdu4311 曼哈顿距离
hdu4311 曼哈顿距离
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
4 6 -4 -1 -1 -2 2 -4 0 2 0 3 5 -2 6 0 0 2 0 -5 -2 2 -2 -1 2 4 0 5 -5 1 -1 3 3 1 3 -1 1 -1 10 -1 -1 -3 2 -4 4 5 2 5 -4 3 -1 4 3 -1 -2 3 4 -2 2
Sample Output
26 20 20 56HintIn 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; }
hdu4311 曼哈顿距离
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。