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. python --> 递归 以及装饰器

    一.递归知识 函数迭套执行,逐层执行之后,满足某个条件之后就会停止执行,将return值返回上层的函数,上层函数再逐层返回,最终返回给最初始函数. 递归在斐波那契数列的应用[斐波那契数列特点:前两个数 ...

  2. mysql存储过程和存储函数

    mysql存储过程和存储函数 存数函数代码示例: DROP PROCEDURE IF EXISTS calc_ci_day_suc_rate; delimiter // CREATE FUNCTION ...

  3. php使用openssl进行Rsa长数据加密(117)解密(128) 和 DES 加密解密

    PHP使用openssl进行Rsa加密,如果要加密的明文太长则会出错,解决方法:加密的时候117个字符加密一次,然后把所有的密文拼接成一个密文:解密的时候需要128个字符解密一下,然后拼接成数据. 加 ...

  4. java基础 常用组件

    几个常用组件: 在图形用户界面编程中,我们常常会提供用户登陆界面,比如登陆到会员管理系统,登陆到工资管理系统,仓库管理系统等,如下图我们就会用到: 1. 文本框(JTextField) 2. 密码框( ...

  5. The specified module could not be found

    打开IIS 信息服务,在左侧找到自己的计算机,点右键,选择属性,在主属性中选编辑,打开“目录安全性”选项卡,单击“匿名访问和验证控制”里的“编辑”按钮,在弹出的对话框中确保只选中了“匿名访问”和“集成 ...

  6. HTML 接收本地文件

    HTML代码请把文件拖到下面的框里触发drop事件读取拖放的文件常用情况:结合XMLHttpRequest和拖放文件实现上传查看和管理本地文件和图片 <!DOCTYPE HTML> < ...

  7. 利用SQLite_Expert实现Excel表转SqLite数据库

    1.保留excel数据中需要的字段,删除无关字段. 2.将excel另存为cvs格式文件.<另存名称如:jizhan.cvs 则导入后表名即为jizhan> 3.看下图,新建数据库,命名为 ...

  8. ubuntu-docker-etcd-swarm-shipyard-portainer

    --- env --- root@node1:~# cat /etc/issueUbuntu 12.04.4 LTS \n \l root@node1:~# docker -vDocker versi ...

  9. java---构造器

    public class SomeTrying{ public static void main(String[] args){ new Son(); new Son().Father(); } } ...

  10. maven打包

    1.利用maven-jar-plugin <build> <plugins> <!-- The configuration of maven-jar-plugin --& ...