Meeting point-1
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 56#include<stdio.h> #include<algorithm> #include<math.h> #define maxint 100001 struct ss { __int64 x; __int64 y; }tt[maxint]; bool cmp(ss t1,ss t2) { if(t1.x==t2.x) return t1.y<t2.y; return t1.x<t2.x; } __int64 fx[maxint],fy[maxint],sum[maxint],r[maxint]; bool cmd(int a,int b) { return tt[a].y<tt[b].y; } int main() { int ncase,i,n; scanf("%d",&ncase); while(ncase--) { scanf("%d",&n); fx[0]=0;fy[0]=0; for(i=0;i<n;i++) { scanf("%I64d%I64d",&tt[i].x,&tt[i].y); fx[i]=0;fy[i]=0; r[i]=i; } std::sort(tt,tt+n,cmp); std::sort(r,r+n,cmd);//按照y坐标的大小排序。 for(i=0;i<n;i++) { fx[0]+=tt[i].x-tt[0].x; fy[r[0]]+=tt[r[i]].y-tt[r[0]].y; } for(i=1;i<n;i++) { fx[i]=fx[i-1]+(2*i-n)*(tt[i].x-tt[i-1].x); //此为x的结果及序; fy[r[i]]=fy[r[i-1]]+(2*i-n)*(tt[r[i]].y-tt[r[i-1]].y); //因为x,y要相关联。 } for(i=0;i<n;i++) sum[i]=fx[i]+fy[i]; std::sort(sum,sum+n); printf("%I64d\n",sum[0]); } return 0; }
本文探讨了如何为所有天津大学 ACM 退休成员选择一个聚会地点,以最小化他们从各自居住地到该地点的总旅行时间。通过在无限整数网格上分析成员的居住位置,提出了一个算法来找到最佳会合点。
2755

被折叠的 条评论
为什么被折叠?



