hdu4311(排序)

202 篇文章 0 订阅
42 篇文章 0 订阅

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) 


题意:给出很多点,求一个点到所有点的最短距离。

用排序来解决,很巧妙。


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
struct data
{
    long long x,y;
    int zb;
}a[100010];

bool cmpx(data a,data b)
{
    if(a.x!=b.x)
    return a.x<b.x;
    return a.y<b.y;
}

bool cmpy(data a,data b)
{
    if(a.y!=b.y)
    return a.y<b.y;
    return a.x<b.x;
}


long long j(long long &a,long long &b)
{
    if(a>b)
    return a-b;
    return b-a;
}
int n;
long long sumy[100010],sumx[100010];
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        sumx[0]=sumy[0]=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%I64d%I64d",&a[i].x,&a[i].y);
        sort(a+1,a+n+1,cmpy);
        for(int i=1;i<=n;i++)
        {
            a[i].zb=i;
            sumy[i]=sumy[i-1]+a[i].y;
        }
        sort(a+1,a+n+1,cmpx);
        for(int i=1;i<=n;i++)
            sumx[i]=sumx[i-1]+a[i].x;
        long long ans=999999999999999999;
        for(int i=1;i<=n;i++)
        {
            long long t=0;
            t+=(i-1)*a[i].x-sumx[i-1]+sumx[n]-sumx[i]-(n-i)*a[i].x;

            int j=a[i].zb;
            //cout<<j<<':'<<endl;
           // cout<<t<<' ';
            t+=(j-1)*a[i].y-sumy[j-1]+sumy[n]-sumy[j]-(n-j)*a[i].y;
            //cout<<(j-1)*a[i].y-sumy[j-1]+sumy[n]-sumy[j]-(n-j)*a[i].y<<endl;
            if(ans>t)ans=t;
        }
        cout<<ans<<endl;
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值