Title: Http://acm.hdu.edu.cn/showproblem.php?pid=4311Meeting point-1
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3426 Accepted Submission (s): 1131
Problem DescriptionIt has been ten years since TJU-ACM established. 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 could be hard-to-find out where to Celebrat E This meeting in order to minimize the sum travel time of the the retired tju-acmers.
There is a infinite integer grid at which N retired Tju-acmers has their houses on. They decide to unite at a common meeting place, which was someone ' s house. From any given cell, only 4 adjacent cells is reachable in 1 unit of time.
Eg: (x, Y) can is reached from (X-1,y), (X+1,y), (x, Y-1), (×, y+1).
Finding a common meeting place which minimizes the sum of the travel time of all the retired tju-acmers.
Inputthe first line is a integer t represents there is t test cases. (0<t <=10)
For each test case, the first line is a integer n represents there is n retired tju-acmers. (0<n<=100000), the following n lines each contains a integers x, y coordinate of the i-th Tju-acmer. ( -10^9 <= x, y <= 10^9)
Outputfor each test case, output the minimal sum of travel times.
Sample input46-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 Output26202056
HintIn the first case, the meeting point is ( -1,-2); The second is (0,0), the third are (3,1) and the Last is ( -2,2)
Authortju
Source2012 multi-university Training Contest 2
Recommendzhuyuanchen520 | We have a carefully selected several similar problems for you:4315 4318 4310 4313 4314 Test instructions: give you the coordinates of n points, let you find one of the coordinates, so that all points to that point of the Harman Distance and minimum. The puzzle: The Manhattan distance + prefix and the x and Y are sorted separately, then the prefix and the prefix and the prefixes are maintained after sorting separately. Note Open Long Long
1#include <bits/stdc++.h>2 using namespacestd;3 #defineMAXN 1000104 #defineINF 10000000000000000LL5 #defineLL Long Long6 LL QX[MAXN],QY[MAXN],QX1[MAXN],QY1[MAXN];7 LL ANS[MAXN];8 structnode9 {Ten intA,id; One }X[MAXN],Y[MAXN]; A intRead () - { - ints=0, fh=1;CharCh=GetChar (); the while(ch<'0'|| Ch>'9'){if(ch=='-') fh=-1; ch=GetChar ();} - while(ch>='0'&&ch<='9') {s=s*Ten+ (ch-'0'); ch=GetChar ();} - returns*fh; - } + BOOLCMP (node Aa,node BB) - { + returnaa.a<BB.A; A } at intMain () - { - intt,n,i; - LL mn,sum,sum1; -t=read (); - while(t--) in { -n=read (); to for(i=1; i<=n;i++) {X[i].a=read (), Y[i].a=read (); x[i].id=i;y[i].id=i;} +Sort (x+1, x+n+1, CMP); -Sort (y+1, y+n+1, CMP); theqx[1]=0; qy[1]=0; qx1[1]=0; qy1[1]=0; * for(i=2; i<=n;i++) $ {Panax Notoginsengqx[i]=qx[i-1]+ (LL) (x[i].a-x[i-1].a); -qx1[i]=qx1[i-1]+Qx[i]; theqy[i]=qy[i-1]+ (LL) (y[i].a-y[i-1].a); +qy1[i]=qy1[i-1]+Qy[i]; A } thememset (ans,0,sizeof(ans)); + for(i=1; i<=n;i++) - { $sum=qx[i]* (I-1)-qx1[i-1]+qx1[n]-qx1[i]-qx[i]* (ni); $sum1=qy[i]* (I-1)-qy1[i-1]+qy1[n]-qy1[i]-qy[i]* (ni); -ans[x[i].id]+=sum; -ans[y[i].id]+=sum1; the } -mn=INF;Wuyi for(i=1; i<=n;i++) mn=min (mn,ans[i]); theprintf"%lld\n", MN); - } Wu return 0; -}
Hdu 4311-meeting point-1 manhattan distance, prefix and