Hdu 4312-meeting point-2 Chebyshev distance, Manhattan distance, prefix and

Source: Internet
Author: User

Title: Http://acm.hdu.edu.cn/showproblem.php?pid=4312Meeting point-2

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1231 Accepted Submission (s): 691


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.

It's an opportunity to show yourself in front of your predecessors!

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, all 8 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), (x-1,y+1), (x-1,y-1), (x+1,y+1), (x+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 Output20151438 HintIn the first case, the meeting point is (0,2); The second is (0,0), the third are (1,-1) and the Last is ( -1,-1)

Authortju

Source2012 multi-university Training Contest 2

Recommendzhuyuanchen520 | We have carefully selected several similar problems for you:4318 4313 4314 4315 4316 Test instructions: Give n points, each point can go to the adjacent 8 direction of the lattice, and each move distance is 1, find one of the points so that all points to this point distance and shortest. Chebyshev distance + Manhattan distance + prefix and first of all, we can see that the shortest distance for any two points is the horizontal ordinate difference, and then take the maximum value in absolute values. And this distance is the Chebyshev distance. Then we turn the origin (x, y) into (x+y,x-y) and turn it into a Manhattan distance. Then use the prefix and maintenance. Finally divide the answer by 2. (or the Origin (x, Y) to ((X+y)/2, (x-y)/2), and finally 2, can be. )
1#include <bits/stdc++.h>2 using namespacestd;3 #defineLL Long Long4 #defineMAXN 1000105 #defineINF 1000000000000000LL6 #defineULL unsigned long Long7 structnode8 {9 LL a1,b1;Ten }X[MAXN],Y[MAXN]; One ULL QZX[MAXN],QZY[MAXN],Q1[MAXN],Q2[MAXN],ANS[MAXN]; A LL Read () - { -LL s=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.a1<bb.a1; A } at intMain () - { - LL n,i,x1,y1,x2,y2,dis,t; - ULL sum,sum1,mn; -t=read (); -      while(t--) in     { -n=read (); to          for(i=1; i<=n;i++) {x[i].a1=read (); Y[i].a1=read (); x1= (x[i].a1+y[i].a1); y1= (x[i].a1-y[i].a1); x[i].a1=x1;y[i].a1=y1;x[ i].b1=y[i].b1=i;} +Sort (x+1, x+n+1, CMP); -Sort (y+1, y+n+1, CMP); theqzx[1]=qzy[1]=q1[1]=q2[1]=0; *          for(i=2; i<=n;i++) qzx[i]=qzx[i-1]+ (x[i].a1-x[i-1].a1); $          for(i=2; i<=n;i++) q1[i]=q1[i-1]+Qzx[i];Panax Notoginseng          for(i=2; i<=n;i++) qzy[i]=qzy[i-1]+ (y[i].a1-y[i-1].a1); -          for(i=2; i<=n;i++) q2[i]=q2[i-1]+Qzy[i]; themn=INF; +memset (ans,0,sizeof(ans)); A          for(i=1; i<=n;i++) the         { +sum=0; -sum+= (qzx[i]* (i-1)-q1[i-1]); $sum+= (q1[n]-q1[i]-(n-i) *qzx[i]); $sum1=0; -sum1+= (qzy[i]* (i-1)-q2[i-1]); -sum1+= (q2[n]-q2[i]-(n-i) *qzy[i]); theans[x[i].b1]+=sum; -ans[y[i].b1]+=sum1;Wuyi         } the          for(i=1; i<=n;i++) mn=min (mn,ans[i]); -printf"%lld\n", mn/2LL); Wu     } -     return 0; About}

Hdu 4312-meeting point-2 Chebyshev distance, Manhattan distance, prefix and

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.