Hdu 4312-Meeting point-2 切比雪夫距离,曼哈顿距离,前缀和

时间:2023-03-09 06:26:54
Hdu 4312-Meeting point-2  切比雪夫距离,曼哈顿距离,前缀和

题目: http://acm.hdu.edu.cn/showproblem.php?pid=4312

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

Author
TJU
Source
Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  4318 4313 4314 4315 4316 
题意:给出n个点,每个点可以到相邻的8个方向的格子,且每次移动距离为1,找出其中一个点,使所有点到这个点的距离和最短。
切比雪夫距离+曼哈顿距离+前缀和
首先,我们可以看出,任意两点的最短距离为横纵坐标分别作差,然后取绝对值中的最大值。
而这个距离就是切比雪夫距离。
然后我们把原点(x,y)化为(x+y,x-y),就变成了求曼哈顿距离。
然后用前缀和维护一下即可。
最后把答案除以2即可。(或者把原点(x,y)化为((x+y)/2,(x-y)/2),最后不除2,也可以。)
 #include<bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 100010
#define INF 1000000000000000LL
#define ULL unsigned long long
struct node
{
LL a1,b1;
}x[MAXN],y[MAXN];
ULL qzx[MAXN],qzy[MAXN],q1[MAXN],q2[MAXN],ans[MAXN];
LL read()
{
LL s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
bool cmp(node aa,node bb)
{
return aa.a1<bb.a1;
}
int main()
{
LL n,i,x1,y1,x2,y2,dis,T;
ULL sum,sum1,MN;
T=read();
while(T--)
{
n=read();
for(i=;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+,x+n+,cmp);
sort(y+,y+n+,cmp);
qzx[]=qzy[]=q1[]=q2[]=;
for(i=;i<=n;i++)qzx[i]=qzx[i-]+(x[i].a1-x[i-].a1);
for(i=;i<=n;i++)q1[i]=q1[i-]+qzx[i];
for(i=;i<=n;i++)qzy[i]=qzy[i-]+(y[i].a1-y[i-].a1);
for(i=;i<=n;i++)q2[i]=q2[i-]+qzy[i];
MN=INF;
memset(ans,,sizeof(ans));
for(i=;i<=n;i++)
{
sum=;
sum+=(qzx[i]*(i-)-q1[i-]);
sum+=(q1[n]-q1[i]-(n-i)*qzx[i]);
sum1=;
sum1+=(qzy[i]*(i-)-q2[i-]);
sum1+=(q2[n]-q2[i]-(n-i)*qzy[i]);
ans[x[i].b1]+=sum;
ans[y[i].b1]+=sum1;
}
for(i=;i<=n;i++)MN=min(MN,ans[i]);
printf("%lld\n",MN/2LL);
}
return ;
}