HDU 4311 Prefixes and

Source: Internet
Author: User

Description

It 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.

Input

The 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)

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

26202056         

Hint

In 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) test instructions: N people in different places, choose one of the rooms, so that other people go to the distance of this place and the shortest problem: the distance from the topic is Manhattan distance | x1-x2|+|y1-y2|; So you can consider the x, y, separate calculation, and finally take the max; How to quickly handle the distance from other positions to I position dis[i]? First, the coordinates are ordered to record the prefix and the distance between the two i,j=i+1 that can be found in the adjacent position dis[j]=dis[i]+ (j-2) * (Sumx[j]-sumx[i])-(N-J) * (Sumx[j]-sumx[i]) =dis[i]+ ( I-N) * (Sumx[j]-sumx[i])
1 /******************************2 code by drizzle3 blog:www.cnblogs.com/hsd-/4 ^ ^    ^ ^5 o o6 ******************************/7 //#include <bits/stdc++.h>8#include <iostream>9#include <cstring>Ten#include <cstdio> One#include <map> A#include <algorithm> -#include <cmath> - #definell Long Long the #definePI ACOs (-1.0) - #defineMoD 1000000007 - intT; - using namespacestd; + structnode - { + ll X,y,pos; A} n[100005]; atll sumx[100005]; -ll sumy[100005]; -ll ans1[100005]; -ll ans2[100005]; - intEXM; - BOOLCMP1 (structNode AA,structnode BB) in { -     returnaa.x<bb.x; to } + BOOLCMP2 (structNode AA,structnode BB) - { the     returnaa.y<bb.y; * } $ intMain ()Panax Notoginseng { -      while(~SCANF ("%d",&t)) the     { +          for(intI=1; i<=t; i++) A         { thescanf"%d",&EXM); +memset (N,0,sizeof(N)); -memset (ANS1,0,sizeof(ans1)); $memset (Ans2,0,sizeof(ANS2)); $              for(intj=1; j<=exm; J + +) -             { -scanf"%i64d%i64d",&n[j].x,&n[j].y); then[j].pos=J; -             }WuyiSort (n+1, n+1+exm,cmp1); thesumx[1]=0; -              for(intj=2; j<=exm; J + +) Wu             { -sumx[j]=sumx[j-1]+n[j].x-n[j-1].x; Aboutans1[n[1].pos]+=Sumx[j]; $             } -              for(intj=2; j<=exm; J + +) -             { -  Aans1[n[j].pos]=ans1[n[j-1].pos]-(EXM-J) * (sumx[j]-sumx[j-1]) + (J-2) * (sumx[j]-sumx[j-1]); +             } theSort (n+1, n+1+exm,cmp2); -sumy[1]=0; $              for(intj=2; j<=exm; J + +) the             { thesumy[j]=sumy[j-1]+n[j].y-n[j-1].y; theans2[n[1].pos]+=Sumy[j]; the             } -              for(intj=2; j<=exm; J + +) in             { theans2[n[j].pos]=ans2[n[j-1].pos]-(EXM-J) * (sumy[j]-sumy[j-1]) + (J-2) * (sumy[j]-sumy[j-1]); the             } Aboutll maxn=ans1[1]+ans2[1]; the              for(intj=1; j<=exm; J + +) the             { the                 if(ans1[j]+ans2[j]<MAXN) +                 { -maxn=ans1[j]+Ans2[j]; the                 }Bayi             } theprintf"%i64d\n", MAXN); the         } -     } -     return 0; the}

HDU 4311 Prefixes 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.