题目: http://acm.hdu.edu.cn/showproblem.php?pid=4312

Meeting point-2

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

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.

It's an opportunity to show yourself in front of your predecessors!

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, all 8 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), (x-1,y+1), (x-1,y-1), (x+1,y+1), (x+1,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
20
15
14
38

Hint

In the first case, the meeting point is (0,2); the second is (0,0), the third is (1,-1) and the last is (-1,-1)

 
Author
TJU
 
Source
 
Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  4318 4313 4314 4315 4316 
 
题意:给出n个点,每个点可以到相邻的8个方向的格子,且每次移动距离为1,找出其中一个点,使所有点到这个点的距离和最短。
切比雪夫距离+曼哈顿距离+前缀和
首先,我们可以看出,任意两点的最短距离为横纵坐标分别作差,然后取绝对值中的最大值。
而这个距离就是切比雪夫距离。
然后我们把原点(x,y)化为(x+y,x-y),就变成了求曼哈顿距离。
然后用前缀和维护一下即可。
最后把答案除以2即可。(或者把原点(x,y)化为((x+y)/2,(x-y)/2),最后不除2,也可以。)
 #include<bits/stdc++.h>
using namespace std;
#define LL long long
#define MAXN 100010
#define INF 1000000000000000LL
#define ULL unsigned long long
struct node
{
LL a1,b1;
}x[MAXN],y[MAXN];
ULL qzx[MAXN],qzy[MAXN],q1[MAXN],q2[MAXN],ans[MAXN];
LL read()
{
LL s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
bool cmp(node aa,node bb)
{
return aa.a1<bb.a1;
}
int main()
{
LL n,i,x1,y1,x2,y2,dis,T;
ULL sum,sum1,MN;
T=read();
while(T--)
{
n=read();
for(i=;i<=n;i++){x[i].a1=read();y[i].a1=read();x1=(x[i].a1+y[i].a1);y1=(x[i].a1-y[i].a1);x[i].a1=x1;y[i].a1=y1;x[i].b1=y[i].b1=i;}
sort(x+,x+n+,cmp);
sort(y+,y+n+,cmp);
qzx[]=qzy[]=q1[]=q2[]=;
for(i=;i<=n;i++)qzx[i]=qzx[i-]+(x[i].a1-x[i-].a1);
for(i=;i<=n;i++)q1[i]=q1[i-]+qzx[i];
for(i=;i<=n;i++)qzy[i]=qzy[i-]+(y[i].a1-y[i-].a1);
for(i=;i<=n;i++)q2[i]=q2[i-]+qzy[i];
MN=INF;
memset(ans,,sizeof(ans));
for(i=;i<=n;i++)
{
sum=;
sum+=(qzx[i]*(i-)-q1[i-]);
sum+=(q1[n]-q1[i]-(n-i)*qzx[i]);
sum1=;
sum1+=(qzy[i]*(i-)-q2[i-]);
sum1+=(q2[n]-q2[i]-(n-i)*qzy[i]);
ans[x[i].b1]+=sum;
ans[y[i].b1]+=sum1;
}
for(i=;i<=n;i++)MN=min(MN,ans[i]);
printf("%lld\n",MN/2LL);
}
return ;
}

Hdu 4312-Meeting point-2 切比雪夫距离,曼哈顿距离,前缀和的更多相关文章

  1. HDU 4312 Meeting point-2(切比雪夫距离转曼哈顿距离)

    http://acm.hdu.edu.cn/showproblem.php?pid=4312 题意:在上一题的基础上,由四个方向改为了八个方向. 思路: 引用自http://blog.csdn.net ...

  2. HDU 4311 Meeting point-1 && HDU 4312 Meeting point-2

    这俩个题  题意::给出N(<1e5)个点求找到一个点作为聚会的地方,使每个点到达这里的距离最小.4311是 曼哈顿距离 4312是 切比雪夫距离: 曼哈顿距离 :大家都知道 对于二维坐标系a( ...

  3. BZOJ - 3170: 松鼠聚会 (切比雪夫转曼哈顿距离)

    pro:  有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1.现在N个松鼠要走到一个松鼠家去,求走过的最短距离.0&l ...

  4. BZOJ3170 [Tjoi2013]松鼠聚会 切比雪夫距离 - 曼哈顿距离 - 前缀和

    BZOJ3170 题意: 有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1.现在N个松鼠要走到一个松鼠家去,求走过的最 ...

  5. Q - Euclid in Manhattan(欧几里德距离==曼哈顿距离?)

    Desciption Consider a set of n points in a 2-D plane with integer coordinates. There are various way ...

  6. K-means真的不能使用曼哈顿距离吗?

    问题 说到k-means聚类算法,想必大家已经对它很熟悉了,它是基于距离计算的经典无监督算法,但是有一次在我接受面试时,面试官问了我一个问题:“k-means为什么不能使用曼哈顿距离计算,而使用欧式距 ...

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

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

  8. HDU 4311 Meeting point-1(曼哈顿距离最小)

    http://acm.hdu.edu.cn/showproblem.php?pid=4311 题意:在二维坐标中有n个点,现在要从这n个点中选出一个点,使得其他点到该点的曼哈顿距离总和最小. 思路: ...

  9. 【HDU 4311】Meeting point-1(前缀和求曼哈顿距离和)

    题目链接 正经解法: 给定n个点的坐标,找一个点,到其他点的曼哈顿距离之和最小.n可以是100000.大概要一个O(nlogn)的算法.算曼哈顿距离可以把x和y分开计算排好序后计算前缀和就可以在O(1 ...

随机推荐

  1. References & the Copy-Constructor

    1 There are certain rules when using references: (Page 451) A reference must be initialized when it ...

  2. discuz注册 内部错误

    ucenter整合后,在论坛注册时出现 内部错误,无法显示,这是因为ucenter中,某个appid没写入! 可以把ucenter安装包下的utilities/checkappid.php,复制到uc ...

  3. 进程显示,删除,调度 ps, top kill

    ps:查看进程的情况,显示的是某一时间进程的运行状态.ps --help top:也是查看进程的情况,动态显示进程信息! kill:杀死进程的情况, sudo kill --help 查看相关参数 c ...

  4. SqlServer日期(convert函数,getdate函数)

    SqlServer日期(convert函数,getdate函数) 函数GETDATE()的返回值在显示时只显示到秒.实际上,SQL Sever内部时间可以精确到毫秒级(确切地说,可以精确到3.33毫秒 ...

  5. 移动web问题小结

    Meta标签: <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalab ...

  6. mysql自动备份数据库

    可以选择设置需要备份的库,自动备份压缩,自动删除 7 天前的备份,需要使用 crontab 定时执行. #!/bin/bash # 要备份的数据库名,多个数据库用空格分开 databases=(db1 ...

  7. mvc中的OutputCache

    mvc4中有一个标记属性OutputCache,用来对ActionResult结果进行缓存,如何理解呢?概括地说,就是当你的请求参数没有发生变化时,直接从缓存中取结果,不会再走服务端的Action代码 ...

  8. (转) 各种好用的插件 Xcode

    时间就是金钱.编码效率的提升意味着更多的收入.可是当我们的开发技巧已经到达一定高度时,如何让开发效率更上一层楼呢?答案就是使用开发工具!在这篇文章中,我会向你介绍一些帮助我提升编码速度和工作效率的工具 ...

  9. Tomcat基础教程(四)

    一.将Web应用部署到Tomcat中 为什么要部署?将Web应用部署到Tomcat中,那么Tomcat就能找到相应的Web应用,当Tomcat启动时就会加载和初始化Web应用,而在Tomcat启动后, ...

  10. 一个奇怪的编码 big5-hkscs

    # --*-- coding:utf-8 --*-- import urllib2 import urllib postDict = { 'IsExist_Slt_Part_Id': 'False', ...