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);
}
}

某个点到其他点的曼哈顿距离之和最小(HDU4311)的更多相关文章

  1. Hdu4311 || 4312Meeting point-1/-2 n个点中任意选一个点使得其余点到该点曼哈顿距离之和最小

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  2. hdu 4311 & 4312 Meeting point 曼哈顿距离之和最小

    hdu 4311 题意 平面上\(n(n\leq 1e5)\)个点,找一个点到其它所有点的曼哈顿距离之和最小. 思路 如果是找一个坐标使得所有点到其曼哈顿距离之和最小,那么将\(n\)个横坐标排个序, ...

  3. HDU 4311 Meeting point-1 求一个点到其它点的曼哈顿距离之和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4311 解题报告:在一个平面上有 n 个点,求一个点到其它的 n 个点的距离之和最小是多少. 首先不得不 ...

  4. 51Nod 1108 距离之和最小 V2 1096 距离之和最小 中位数性质

    1108 距离之和最小 V2基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注三维空间上有N个点, 求一个点使它到这N个点的曼哈顿距离之和最小,输出这个最小 ...

  5. 51nod1110 距离之和最小 V3

    基准时间限制:1 秒 空间限制:131072 KB 分值: 40  X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].该点到其他点的带权距离 = 实际距离 * 权值.求X轴上 ...

  6. 51nod 1096 距离之和最小 1108 距离之和最小 V2

    [题解] 很显然在一条坐标轴上到各个点距离之和最小的点就是它们的中位数.怎么证明呢?我们假设现在找的某个点x左边有a个点,右边有b个点(a>b).我们把x向左移动d个单位,并保证x左边依然有a个 ...

  7. 1110 距离之和最小 V3

    1110 距离之和最小 V3 基准时间限制:1 秒 空间限制:131072 KB X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].该点到其他点的带权距离 = 实际距离 * ...

  8. 51Nod 1110 距离之和最小 V3 中位数 思维

    基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].点P到点P[i]的带权距离 = 实际距离 ...

  9. 【51NOD】1096 距离之和最小

    [算法]数学 [题解] 其实就是求中位数,奇数个点就是最中间的点,偶数个点就是最中间两个点和它们之间的区域皆可(所以偶数不必取到两点正中央,取两点任意一点即可). 我们可以想象现在x轴上有n个点,我们 ...

随机推荐

  1. P2661 信息传递 TODO-TARJAN算法

    http://www.cnblogs.com/zbtrs/p/5762788.html http://blog.csdn.net/loi_yzs/article/details/52795093 都是 ...

  2. Delphi结构体数组指针的问题

    //这段代码在Delphi 2007和delphi 7下是可以执行的,所以正确使用结构体数组和指针应该是这样的,已验证 unit Unit1; interface uses Windows, Mess ...

  3. Oracle存储过程java 调用

    1.nest表组合成结果集,然后以游标变量的形式返回 --创建类型 create or replace package mytest is -- Author  : ADMINISTRATOR  -- ...

  4. css中IE6fix问题

    .g-popup-mask, .g-popup-box { position:fixed; top:0; left:0; z-index:10000; _position:absolute; _top ...

  5. 一本很不错的书----DOOM启示录

    强推,所有玩游戏的和做游戏的热爱游戏的都应该看看. 摘录了一些话. 盖茨不明白,为什么啊为什么,为什么一个麦斯奎特的小公司,居然能从他手下挖走迈克尔·亚伯拉什,而且仅仅凭借几个游戏就胜过了自己的软件帝 ...

  6. Jquery下拉效果

    $('#触发元素').hover(function(){ $('#框框').slideDown(); //展开(动画效果)},function(){ $('#框框').slideUp(); //收起( ...

  7. sqlserver 视图能否有变量

    不能,sqlserver 视图一般不能有变量,也不能带存储过程

  8. thinkPHP 接支付宝及时到账接口

    支付宝及时到帐接口,现在整理以下: 1.先将支付宝提供的公共类库函数库文件防盗thinkPHP的Vender目录下建的一个alipay文件下,以便之后的调用. //四个文件我分别给他们改了下名字,因为 ...

  9. 【转】Android Paint之 setXfermode PorterDuffXfermode 讲解

    [置顶] Android Paint之 setXfermode PorterDuffXfermode 讲解 分类: android动效篇2015-04-07 17:23 978人阅读 评论(8) 收藏 ...

  10. Flash的坑之ExternalInterface.call只返回null值的解决办法

    flash坑太多了,要确保能有效的使用ExternalInterface.call调用js的话,需要两个条件: 1.allowScriptAccess="always" 2.id= ...