Hdu 4312-Meeting point-2 切比雪夫距离,曼哈顿距离,前缀和
Posted 微弱的世界
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.
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)
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,也可以。)
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 #define MAXN 100010 5 #define INF 1000000000000000LL 6 #define ULL unsigned long long 7 struct node 8 { 9 LL a1,b1; 10 }x[MAXN],y[MAXN]; 11 ULL qzx[MAXN],qzy[MAXN],q1[MAXN],q2[MAXN],ans[MAXN]; 12 LL read() 13 { 14 LL s=0,fh=1;char ch=getchar(); 15 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)fh=-1;ch=getchar();} 16 while(ch>=‘0‘&&ch<=‘9‘){s=s*10+(ch-‘0‘);ch=getchar();} 17 return s*fh; 18 } 19 bool cmp(node aa,node bb) 20 { 21 return aa.a1<bb.a1; 22 } 23 int main() 24 { 25 LL n,i,x1,y1,x2,y2,dis,T; 26 ULL sum,sum1,MN; 27 T=read(); 28 while(T--) 29 { 30 n=read(); 31 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;} 32 sort(x+1,x+n+1,cmp); 33 sort(y+1,y+n+1,cmp); 34 qzx[1]=qzy[1]=q1[1]=q2[1]=0; 35 for(i=2;i<=n;i++)qzx[i]=qzx[i-1]+(x[i].a1-x[i-1].a1); 36 for(i=2;i<=n;i++)q1[i]=q1[i-1]+qzx[i]; 37 for(i=2;i<=n;i++)qzy[i]=qzy[i-1]+(y[i].a1-y[i-1].a1); 38 for(i=2;i<=n;i++)q2[i]=q2[i-1]+qzy[i]; 39 MN=INF; 40 memset(ans,0,sizeof(ans)); 41 for(i=1;i<=n;i++) 42 { 43 sum=0; 44 sum+=(qzx[i]*(i-1)-q1[i-1]); 45 sum+=(q1[n]-q1[i]-(n-i)*qzx[i]); 46 sum1=0; 47 sum1+=(qzy[i]*(i-1)-q2[i-1]); 48 sum1+=(q2[n]-q2[i]-(n-i)*qzy[i]); 49 ans[x[i].b1]+=sum; 50 ans[y[i].b1]+=sum1; 51 } 52 for(i=1;i<=n;i++)MN=min(MN,ans[i]); 53 printf("%lld\n",MN/2LL); 54 } 55 return 0; 56 }
以上是关于Hdu 4312-Meeting point-2 切比雪夫距离,曼哈顿距离,前缀和的主要内容,如果未能解决你的问题,请参考以下文章
4_InfluxDB学习之InfluxDB的基本概念InfluxDB中独有的概念(Point,series),InfluxDB学习之InfluxDB的基本操作,InfluxDB操作方式,crud(代码