某个点到其他点的曼哈顿距离之和最小(HDU4311)

Meeting point-1

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


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. 
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.
 

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
  
  
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个点的坐标,要求选择一个点作为中心,使其他点到该店的曼哈顿距离之和最小:
分析;首先把x和y坐标分离出来,分别从小到大排序,然后会形成一个绝对值之和,我们知道在某个点的左边求和去绝对值方式,和右边去绝对值求和方式,所以根据特性分别用sumx和sumy记录从左到右到达每个点的坐标和;
例如对于绝对值求和公式 :  
sum=|x+10|+|x+5|+|x+1|+|x-2|+|x-6|;sumx[1]=-10;sumx[2]=-15;sumx[3]=-16;sumx[4]=-14;sumx[5]=-8;  
对于第3个点sum=(-1)*3-sumx[3]+sum[5]-sum[3]-(-1)*(5-3);
然后现行枚举对于每个p[i]找出p[i].x和p[i].y在x[]和y[]数组中所对的下标套用公式即可:
程序:
#include"stdio.h"
#include"string.h"
#include"math.h"
#include"algorithm"
#include"vector"
#include"queue"
#include"stack"
#define M 100009
#define inf 1000000000000000LL
using namespace std;
struct node
{
    __int64 x,y;
}p[M];
int n;
__int64 x[M],y[M],sumx[M],sumy[M];
int cmp(int a,int b)
{
    return a<b;
}
int finde(__int64 *a,__int64 key)
{
    int l=1,r=n,mid;
    while(l<=r)
    {
        mid=(l+r)/2;
        if(a[mid]==key)
            return mid;
        if(a[mid]>key)
            r=mid-1;
        else
            l=mid+1;
    }
    return -1;
}
int main()
{
    int T,i;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%I64d%I64d",&p[i].x,&p[i].y);
            x[i]=p[i].x;
            y[i]=p[i].y;
        }
        sort(x+1,x+n+1,cmp);
        sort(y+1,y+n+1,cmp);
        sumx[0]=sumy[0]=0;
        for(i=1;i<=n;i++)
        {
            sumx[i]=sumx[i-1]+x[i];
            sumy[i]=sumy[i-1]+y[i];
        }
        __int64 ans=inf;
        for(i=1;i<=n;i++)
        {
            int dx=finde(x,p[i].x);
            int dy=finde(y,p[i].y);
            __int64 sx=x[dx]*dx-sumx[dx]+sumx[n]-sumx[dx]-(n-dx)*x[dx];
            __int64 sy=y[dy]*dy-sumy[dy]+sumy[n]-sumy[dy]-(n-dy)*y[dy];
            if(ans>sx+sy)
                ans=sx+sy;
        }
        printf("%I64d\n",ans);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值