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
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.
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)
和 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;
}