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

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
 
题意:比较经典的题,给出n个点的坐标,从中选一个点,使得其余的n-1个点到该点的曼哈顿距离之和最小
曼哈顿距离:对于(x1,y1)和(x2,y2),这两点的曼哈顿距离为abs(x1 - x2) + abs(y1 - y2)
思路:给出一维坐标上的n个数,要求从中找出一点,使得该点到其余点距离之和最小。大白里面有证明这n个数的中为数对应的点就是答案,但是我们仍然可以通过递推解决这个一维的问题,设sl[i]表示i的左边i-1个数到第i个数的距离之和,那么有sl[i] = sl[i-1] + (i-1) * (a[i] - a[i-1]); 同理sr[i]表示i的右边i-1个数到i的距离和,有sr[i] = sr[i+1] + (n-i) * (a[i+1]-a[i]);  这两个递推式成立的前提是序列有序。那么上面一维问题的答案就是min(sl[i] + sr[i]);
原题是2维的,但是我们可以分别考虑x,y,即对于每一个点,求出选该点时其余点到该点x方向的距离和以及y方向的距离和,那么min(ansx+ansy)就是答案
 
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + ;
struct Point {
int x, y, id;
Point() {}
Point(int x, int y, int id) : x(x), y(y), id(id) {}
};
Point p[N];
int n;
ll sl[N], sr[N], ansx[N], ansy[N];
int cmp1(Point a, Point b) { return a.x < b.x; }
int cmp2(Point a, Point b) { return a.y < b.y; }
void initx() {
sort(p, p + n, cmp1);
sl[] = ; sr[n - ] = ;
for(int i = ; i < n; ++i) sl[i] = sl[i - ] + 1ll * i * (p[i].x - p[i - ].x);
for(int i = n - ; i >= ; --i) sr[i] = sr[i + ] + 1ll * (n - i - ) * (p[i + ].x - p[i].x);
for(int i = ; i < n; ++i) ansx[ p[i].id ] = sl[i] + sr[i];
}
void inity() {
sort(p, p + n, cmp2);
sl[] = ; sr[n - ] = ;
for(int i = ; i < n; ++i) sl[i] = sl[i - ] + 1ll * i * (p[i].y - p[i - ].y);
for(int i = n - ; i >= ; --i) sr[i] = sr[i + ] + 1ll * (n - i - ) * (p[i + ].y - p[i].y);
for(int i = ; i < n; ++i) ansy[ p[i].id ] = sl[i] + sr[i];
}
int main() {
// freopen("in.txt", "r", stdin);
int _; scanf("%d", &_);
while(_ --) {
scanf("%d", &n);
for(int i = ; i < n; ++i) {
scanf("%d%d", &p[i].x, &p[i].y);
p[i].id = i;
}
initx();
inity();
ll ans = (1ll << );
for(int i = ; i < n; ++i) ans = min(ansx[i] + ansy[i], ans);
printf("%I64d\n", ans);
}
return ;
}

hdu4312: 给出n个点的坐标,从中选一个点,使得其余的n-1个点到该点的切比雪夫距离之和最小

切比雪夫距离:对于(x1,y1)和(x2,y2),这两点的曼哈顿距离为max(abs(x1 - x2) , abs(y1 - y2))

公式转化:max(abs(x1 - x2) , abs(y1 - y2)) = abs((x1+y1)-(x2+y2)) + abs((x1-y1)-(x2-y2)); 那么另x = x1 + y1; y = x1 - y2; 就和4311完全一样了

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

  1. 某个点到其他点的曼哈顿距离之和最小(HDU4311)

    Meeting point-1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

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

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

  3. [hdu4311]Meeting point-1

    题意:在整数坐标轴上找一个距离所有给定点距离最小的点. 解题关键:对x和y分别处理,前缀和预处理所有点到最小点的距离,每点的$sum$等于左边的贡献+右边的贡献,最后取$min$即可. 复杂度:$O( ...

  4. Hdu 4312-Meeting point-2——哈夫曼距离与切比雪夫距离

    题意 从 $n$ 个点中选择一点,使得其他点到其的切比雪夫距离最小($0 < n \leq 1e5$). 分析 定理:$(x_1, y_1)$ 与 $(x_2, y_2)$ 的曼哈顿距离等于 $ ...

  5. mapreduce中一个map多个输入路径

    package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...

  6. In-Memory:内存数据库

    在逝去的2016后半年,由于项目需要支持数据的快速更新和多用户的高并发负载,我试水SQL Server 2016的In-Memory OLTP,创建内存数据库实现项目的负载需求,现在项目接近尾声,系统 ...

  7. 01.SQLServer性能优化之---水平分库扩展

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 第一次引入文件组的概念:http://www.cnblogs.com/dunitian/ ...

  8. 从直播编程到直播教育:LiveEdu.tv开启多元化的在线学习直播时代

    2015年9月,一个叫Livecoding.tv的网站在互联网上引起了编程界的注意.缘于Pingwest品玩的一位编辑在上网时无意中发现了这个网站,并写了一篇文章<一个比直播睡觉更奇怪的网站:直 ...

  9. Hadoop 中利用 mapreduce 读写 mysql 数据

    Hadoop 中利用 mapreduce 读写 mysql 数据   有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...

随机推荐

  1. javascript 获取滚动条高度+常用js页面宽度与高度

    /******************** * 取窗口滚动条高度  ******************/function getScrollTop(){    var scrollTop=0;    ...

  2. Linux C 收藏

    某招聘要求:熟悉高性能分布式网络服务端设计开发,熟悉epoll.多线程.异步IO.事件驱动等服务端技术: <UNIX环境高级编程(第3版)>apue.h等源码文件的编译安装 <UNI ...

  3. GSM07.10协议中串口复用使用的校验算法

    ] = { 0x00, 0x91, 0xE3, 0x72, 0x07, 0x96, 0xE4, 0x75, 0x0E, 0x9F, 0xED, 0x7C, 0x09, 0x98, 0xEA, 0x7B ...

  4. 如何让两个div在同一行显示?一个float搞定

    最近在学习div和css,遇到了一些问题也解决了很多以前以为很难搞定的问题.比如:如何让两个div显示在同一行呢?(不是用table表格,table对SE不太友好)其实,<div> 是一个 ...

  5. BOM 浏览器对象模型

    总结自JavaSript高级编程

  6. 预处理命令[#define]说明

    宏定义 宏定义是对一些常见的变量.字符串等进行定义,被定义的数据在编译会进行自动替换.有时一些变量或字符串被多次使用,当需要修改时,就需要对源文件中它们出现的地方一一修改,效率比较低,而通过宏定义,只 ...

  7. Gridview中运用CommandField 删除控件时注意点

    我在gridview1 <编辑列>里面添加了一个<CommandField 删除>的控件,之后在gridview1的事件<RowDeleting>事件下 写了一段删 ...

  8. Python目录

  9. Docker - Dockerizing applications: A “Hello world”

    1. 在Docker容器(container)运行Hello world $ docker run ubuntu /bin/echo 'Hello world' docker run - 运行一个容器 ...

  10. NetBeans invalid jdkhome specified 问题解决方法

    JDK的路径变化会导致 NetBeans 启动时出现错误: 解决办法: There's is an easy way to fix this. Navigate to your NetBeans in ...