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

Meeting point-1

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

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)

 
Author
TJU
 
Source
 
Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  4315 4318 4310 4313 4314 
 
题意:给你n个点的坐标,让你找其中一个坐标,使得所有点到那个点的曼哈顿距离和最小。
题解:
曼哈顿距离+前缀和
把x和y分别排序,然后去维护分别排序后的前缀和,还有前缀和的前缀和。
注意开long long
 #include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
#define INF 10000000000000000LL
#define LL long long
LL qx[MAXN],qy[MAXN],qx1[MAXN],qy1[MAXN];
LL ans[MAXN];
struct node
{
int a,id;
}x[MAXN],y[MAXN];
int read()
{
int 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.a<bb.a;
}
int main()
{
int T,n,i;
LL MN,sum,sum1;
T=read();
while(T--)
{
n=read();
for(i=;i<=n;i++){x[i].a=read(),y[i].a=read();x[i].id=i;y[i].id=i;}
sort(x+,x+n+,cmp);
sort(y+,y+n+,cmp);
qx[]=;qy[]=;qx1[]=;qy1[]=;
for(i=;i<=n;i++)
{
qx[i]=qx[i-]+(LL)(x[i].a-x[i-].a);
qx1[i]=qx1[i-]+qx[i];
qy[i]=qy[i-]+(LL)(y[i].a-y[i-].a);
qy1[i]=qy1[i-]+qy[i];
}
memset(ans,,sizeof(ans));
for(i=;i<=n;i++)
{
sum=qx[i]*(i-)-qx1[i-]+qx1[n]-qx1[i]-qx[i]*(n-i);
sum1=qy[i]*(i-)-qy1[i-]+qy1[n]-qy1[i]-qy[i]*(n-i);
ans[x[i].id]+=sum;
ans[y[i].id]+=sum1;
}
MN=INF;
for(i=;i<=n;i++)MN=min(MN,ans[i]);
printf("%lld\n",MN);
}
return ;
}

Hdu 4311-Meeting point-1 曼哈顿距离,前缀和的更多相关文章

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

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

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

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

  3. Hdu 4312-Meeting point-2 切比雪夫距离,曼哈顿距离,前缀和

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=4312 Meeting point-2 Time Limit: 2000/1000 MS (Java/Ot ...

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

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

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

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

  6. hdu 6435 CSGO(最大曼哈顿距离)

    题目链接 Problem Description You are playing CSGO. There are n Main Weapons and m Secondary Weapons in C ...

  7. [HDU 4666]Hyperspace[最远曼哈顿距离][STL]

    题意: 许多 k 维点, 求这些点之间的最远曼哈顿距离. 并且有 q 次操作, 插入一个点或者删除一个点. 每次操作之后均输出结果. 思路: 用"疑似绝对值"的思想, 维护每种状态 ...

  8. HDU - 6435 Problem J. CSGO (曼哈顿距离变换)

    题目大意:有两类武器(主武器和副武器),每类有若干把,每把武器都有一个基础属性S,以及k个附加属性,让你选一把主武器M和一把副武器S,使得最大. 显然后面的和式是一个k维的曼哈顿距离,带绝对值符号不好 ...

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

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

随机推荐

  1. Php之Http请求urlencode/rawurlencode

    在http请求中,如果参数值带中文最好使用urlencode/rawurlencode函数. 如果参数值中带加号也最好使用,否则后台接收时,+号变成空格,引起不必要的麻烦. 注:urlencode和r ...

  2. 使用sqlmap注入DVWA的SQL Injection菜单

    1 使用sqlmap注入DVWA的SQL Injection菜单 本教程中的登陆地址:http://192.168.0.112/dvwa/login.php 1.1 获取cookie信息 1) 使用a ...

  3. [转]Vim 复制粘贴探秘

    Vim作为最好用的文本编辑器之一,使用vim来编文档,写代码实在是很惬意的事情.每当学会了vim的一个新功能,就会很大地提高工作效率.有人使用vim几十年,还没有完全掌握vim的功能,这也说明了vim ...

  4. HTML 页面加载动画效果

    浏览器:Chrome, IE <!doctype html> <html> <head> <title>CSS transform: CSS only ...

  5. Codeforces Problem 598E - Chocolate Bar

    Chocolate Bar 题意: 有一个n*m(1<= n,m<=30)的矩形巧克力,每次能横向或者是纵向切,且每次切的花费为所切边长的平方,问你最后得到k个单位巧克力( k <= ...

  6. C# 两时间,时间间隔

    #region 返回时间差        public static string DateDiff(DateTime DateTime1, DateTime DateTime2)        {  ...

  7. caller和callee的区别

    ①.caller caller返回一个函数的引用,这个函数调用了当前的函数. 使用这个属性要注意: 1 这个属性只有当函数在执行时才有用 2 如果在javascript程序中,函数是由顶层调用的,则返 ...

  8. (00)Java编程思想开篇立言。

    从今天开始,在相当长的时间中我在看Java编程思想.也把这个博客作为开始.这就是一个读书的笔记.

  9. C语言程序设计做题笔记之C语言基础知识(下)

    C 语言是一种功能强大.简洁的计算机语言,通过它可以编写程序,指挥计算机完成指定的任务.我们可以利用C语言创建程序(即一组指令),并让计算机依指令行 事.并且C是相当灵活的,用于执行计算机程序能完成的 ...

  10. BZOJ 1044

    1044: [HAOI2008]木棍分割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1393  Solved: 497[Submit][Statu ...