题意:给出平面坐标系上若干个点,求出一点,使得其他点到其的曼哈顿距离和最小。
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.
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, only 4 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).
Finding a common meeting place which minimizes the sum of the travel time of all the retired TJU-ACMers.
InputThe first line is an integer T represents there are T test cases. (0<T <=10)
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, only 4 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).
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)
OutputFor 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 2Sample Output
26
20
20
56
Hint
In the first case, the meeting point is (-1,-2); the second is (0,0), the third is (3,1) and the last is (-2,2)
数据量为N=100000,时间要求为1000ms,所以我们要在NlogN以下的复杂度下解决此问题。
假设我们某个点为我们要求的点时,那ans=xz+xy+yz+yy。
xz代表只观察X轴,在该点左边的点到该点的距离和。xy则代表只观察X轴,在该点右边的点到该点的距离和。
yz,yy同理。
我们可以依次按照x,y升序,降序这四种排序方式,对平面上的点进行排序。
然后用dp的方式将每个点的xz,xy,yz,yy求出。然后进行循环求出ans的最小值
代码:
//#include<bits/stdc++.h>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
long long xz[100010];
long long xy[100010];
long long yz[100010];
long long yy[100010];
struct point
{
long long x,y;
int statu;
};
point p[100010];
bool cmp1(point a,point b)
{return a.x<b.x;}
bool cmp2(point a,point b)
{return a.x>b.x;}
bool cmp3(point a,point b)
{return a.y<b.y;}
bool cmp4(point a,point b)
{return a.y>b.y;}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%lld%lld",&p[i].x,&p[i].y);
p[i].statu=i;
}
memset(xz,0,sizeof(xz));
memset(xy,0,sizeof(xy));
memset(yz,0,sizeof(yz));
memset(yy,0,sizeof(yz));
sort(p,p+n,cmp1);
for(int i=1;i<n;i++){
int now=p[i].statu;
int be=p[i-1].statu;
xz[now]=xz[be]+i*(p[i].x-p[i-1].x);
//printf("%d %lld\n",now,xz[now]);
}
//for(int i=0;i<n;i++)
//printf("%lld\n",xz[i]);
sort(p,p+n,cmp2);
for(int i=0;i<n;i++){
int now=p[i].statu;
int be=p[i-1].statu;
xy[now]=xy[be]+i*(p[i-1].x-p[i].x);
// printf("%d %lld\n",now,xy[now]);
}
sort(p,p+n,cmp3);
for(int i=1;i<n;i++){
int now=p[i].statu;
int be=p[i-1].statu;
yz[now]=yz[be]+i*(p[i].y-p[i-1].y);
}
sort(p,p+n,cmp4);
for(int i=1;i<n;i++){
int now=p[i].statu;
int be=p[i-1].statu;
yy[now]=yy[be]+i*(p[i-1].y-p[i].y);
}
long long ans=1ll<<63-1;
for(int i=0;i<n;i++)
{
ans=min(ans,xz[i]+xy[i]+yz[i]+yy[i]);
}
printf("%lld\n",ans);
}
return 0;
}