hdu 4312 Meeting point-2

Meeting point-2

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 550    Accepted Submission(s): 322


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. 

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

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, all 8 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), (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.
 

 

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)
 

 

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
20 15 14 38
Hint
In the first case, the meeting point is (0,2); the second is (0,0), the third is (1,-1) and the last is (-1,-1)
 

 

Source
 

 

Recommend
zhuyuanchen520
思路:

和 4311 的区别在于一个转换

max(|x|,|y|) 等于 max(|x-y|,|x+y|)/2

原来的两点 (x1,y1)   (x2,y2)  转换为   (x1-y1,x1+y1)  (x2-y2,x2+y2) 

最后记得除以2

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4312

代码:

#include <iostream>//莫名其秒要用cin,cout输出才对
#include <algorithm>
using namespace std;
#define ll long long
#define maxn 200000
struct Node
{
 ll x;
 ll y;
 int I;
}a[maxn];
ll l[maxn],sumx[maxn],sumy[maxn];
bool cmp1( Node A,Node B)
{
  if(A.x<B.x)
   return true;
   return false;
}
bool cmp2( Node A,Node B)
{
   if(A.y<B.y)
    return true;
    return false;
}
int main( )
{
  int test,n,i;
  ll xx,yy;
  scanf("%d",&test);
  while(test--)
  {
 scanf("%d",&n);
 for( i=1;i<=n;i++)
 {
   cin>>xx>>yy;
  a[i].x=xx-yy;a[i].y=xx+yy;
  a[i].I=i;
 }
 sort(a+1,a+n+1,cmp1);
 l[0]=0;
    for( i=1;i<=n;i++)
  l[i]=l[i-1]+a[i].x;
 for( i=1;i<=n;i++)
 {
   sumx[a[i].I]=(i-1)*a[i].x-l[i-1]+(l[n]-l[i]-(n-i)*a[i].x);
    }
    sort(a+1,a+n+1,cmp2);
    l[0]=0;
    for( i=1;i<=n;i++)
    {
  l[i]=a[i].y+l[i-1];
 }
     for( i=1;i<=n;i++)
 {
   sumy[a[i].I]=(i-1)*a[i].y-l[i-1]+(l[n]-l[i]-(n-i)*a[i].y);
    }
 ll ans=sumx[1]+sumy[1];
 for( i=2;i<=n;i++)
 {
   if(ans>(sumx[i]+sumy[i]))
     ans=sumx[i]+sumy[i];
    }
   cout<<ans/2<<endl;
  }
  return 0;
}

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4312

 
posted @ 2012-07-29 09:03  jiai  Views(221)  Comments(0Edit  收藏  举报