Hdu 4312-Meeting point-2 切比雪夫距离,曼哈顿距离,前缀和
题目: 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
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.
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)
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
15
14
38
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)
#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 切比雪夫距离,曼哈顿距离,前缀和的更多相关文章
- HDU 4312 Meeting point-2(切比雪夫距离转曼哈顿距离)
http://acm.hdu.edu.cn/showproblem.php?pid=4312 题意:在上一题的基础上,由四个方向改为了八个方向. 思路: 引用自http://blog.csdn.net ...
- HDU 4311 Meeting point-1 && HDU 4312 Meeting point-2
这俩个题 题意::给出N(<1e5)个点求找到一个点作为聚会的地方,使每个点到达这里的距离最小.4311是 曼哈顿距离 4312是 切比雪夫距离: 曼哈顿距离 :大家都知道 对于二维坐标系a( ...
- BZOJ - 3170: 松鼠聚会 (切比雪夫转曼哈顿距离)
pro: 有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1.现在N个松鼠要走到一个松鼠家去,求走过的最短距离.0&l ...
- BZOJ3170 [Tjoi2013]松鼠聚会 切比雪夫距离 - 曼哈顿距离 - 前缀和
BZOJ3170 题意: 有N个小松鼠,它们的家用一个点x,y表示,两个点的距离定义为:点(x,y)和它周围的8个点即上下左右四个点和对角的四个点,距离为1.现在N个松鼠要走到一个松鼠家去,求走过的最 ...
- Q - Euclid in Manhattan(欧几里德距离==曼哈顿距离?)
Desciption Consider a set of n points in a 2-D plane with integer coordinates. There are various way ...
- K-means真的不能使用曼哈顿距离吗?
问题 说到k-means聚类算法,想必大家已经对它很熟悉了,它是基于距离计算的经典无监督算法,但是有一次在我接受面试时,面试官问了我一个问题:“k-means为什么不能使用曼哈顿距离计算,而使用欧式距 ...
- hdu 4311 & 4312 Meeting point 曼哈顿距离之和最小
hdu 4311 题意 平面上\(n(n\leq 1e5)\)个点,找一个点到其它所有点的曼哈顿距离之和最小. 思路 如果是找一个坐标使得所有点到其曼哈顿距离之和最小,那么将\(n\)个横坐标排个序, ...
- HDU 4311 Meeting point-1(曼哈顿距离最小)
http://acm.hdu.edu.cn/showproblem.php?pid=4311 题意:在二维坐标中有n个点,现在要从这n个点中选出一个点,使得其他点到该点的曼哈顿距离总和最小. 思路: ...
- 【HDU 4311】Meeting point-1(前缀和求曼哈顿距离和)
题目链接 正经解法: 给定n个点的坐标,找一个点,到其他点的曼哈顿距离之和最小.n可以是100000.大概要一个O(nlogn)的算法.算曼哈顿距离可以把x和y分开计算排好序后计算前缀和就可以在O(1 ...
随机推荐
- 切换到mint了,纪念一下
- discuz xplus 模板 没解析的问题
<?xxx?> 模板中用得是短标签,没加php, 在php.ini中,把 open_short_tag = On ; 打开即可!浪费一上午的时间
- 由json生成php配置文件
$str = '<?php return ' . var_export(json_decode($json, true), true) . ';';file_put_contents('./co ...
- centos7.0安装docker报错
使用centos7.0安装dockers时出现Transaction check error错误. yum install docker Transaction check error: file / ...
- 小笔记(一):ajax传递数组及将ajax返回数据赋值
当使用ajax传递数据时,有可能传递多个数据,这是使用以下方法传递数据就会显得数据过多且混杂 $.ajax({ type:'post', url:url, data:{data:data,conten ...
- iOS Copy 和 MutableCopy的区别 深浅拷贝的区别-供参考
概述 对于系统的非容器类对象,对一不可变对象复制,copy是指针复制(浅拷贝)和mutableCopy就是对象复制(深拷贝).如果是对可变对象复制,都是深拷贝,但是copy返回的对象是不可变的. 对于 ...
- ms-on-input
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- BZOJ 1642: [Usaco2007 Nov]Milking Time 挤奶时间
Description 贝茜是一只非常努力工作的奶牛,她总是专注于提高自己的产量.为了产更多的奶,她预计好了接下来的N (1 ≤ N ≤ 1,000,000)个小时,标记为0..N-1. Farmer ...
- 【技术贴】三星Note8 N5100实用教程,关闭相机快门声,增加浏览器退出按钮。
需要root 增加快门声按钮: 在\system\csc\目录下,有个others.xml的手机功能定制文件,用root explorer之类可以修改系统文件权限的文本修改工具编辑它,在文件最末添加这 ...
- discuz 门户栏目URL跳转异常的问题
“SEO设置-URL静态化”的问题.