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. ifarm 子 父页面方法如何互调

    1.iframe子页面调用父页面js函数 子页面调用父页面函数只需要写上window.praent就可以了.比如调用a()函数,就写成: 代码如下: window.parent.a(); 子页面取父页 ...

  2. filter:Alpha总结

    filter:Alpha(Opacity=?, FinishOpacity=?, Style=?, StartX=?, StartY=?, FinishX=?, FinishY=?)Opacity:透 ...

  3. 删除下标为n的数组值

    Array.prototype.del=function(n) { //n表示第几项,从0开始算起.//prototype为对象原型,注意这里为对象增加自定义方法的方法. if(n<0) //如 ...

  4. CLR调试报错“Visual Studio远程调试监视器 (MSVSMON.EXE) 的 64 位版本无法调试 32 位进程或 32 位转储。请改用 32 位版本”的解决

    Win7 64位电脑上进行visual studio的数据库项目的CLR存储过程进行调试时,报错: ---------------------------Microsoft Visual Studio ...

  5. Linq&Lumbda

    var y = from model in list.Where(s=>s.product==product||product=="")                    ...

  6. MongoDB过过瘾

    MongoDB 中默认的数据库为 test,连接后尝试以下操作 连接 插入数据:用过json的同学看到这格式相信不会陌生吧! db.person.insert({}) db.person.insert ...

  7. Cocos2d-JS cc.DrawNode用法

    app.js var HelloWorldLayer = cc.Layer.extend({ sprite:null, ctor:function () { ///////////////////// ...

  8. magento多语言中文语言包

    语言包key:http://connect20.magentocommerce.com/community/Mage_Locale_zh_CN

  9. Swift-11-协议(Protocols)

    协议定义了一个蓝图,规定了用来实现某一特定工作或者功能所必须的方法和属性.类.结构体或者枚举类型都可以遵循协议,并提供具体实现来完成协议定义的方法和功能.任意能满足协议要求的类型被称为遵循confor ...

  10. Windows-002-U盘启动盘制作

    通常我们安装系统时,均采用光盘的形式安装,只是这种方法需要随时随地的带着光盘,还不容易保存.携带光盘.这时,一个 U盘启动盘 就是您的首选了,此种方式的好处多多,比如:忘记开机密码.系统备份.安装系统 ...