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. 对日期 ,和 master.dbo.spt_values 表操作

    if object_id('tempdb..#xs') is not null drop table #xs; ), date datetime, sale float) insert into #x ...

  2. C++ vector容器find查询函数

    vector< ); //查找1 if ( result == L.end( ) ) //没找到 cout << "No" << endl; else ...

  3. mac 下设置jdk 路径,设置hadoop 路径

    1. touch ~/.bash_profile  创建一个文件 2.vim ~/.bash_profile JAVA_HOME=/Library/Java/JavaVirtualMachines/j ...

  4. python 装饰器的理解

    一. 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身无关的雷 ...

  5. awk

    cat map-matcher.log | awk -F '[' '{print $1}' | awk -F '-' '{print $2}' >result.txt cat 2.txt | a ...

  6. su与su-

    1.Linux中的用户切换:su和su - 的区别 大部分Linux发行版的默认账户是普通用户,而更改系统文件或者执行某些命令,需要root身份才能进行,这就需要从当前用户切换到root用户,Linu ...

  7. Tern Sercer Tineout

  8. Eclipse 一些小知识

    快速查找未完成事项 eg: // TODO 通过模板格式化代码 Window --> Preferences --> Java --> Editor  --> Template ...

  9. iOS开发常用校验

    一.身份证号码校验 + (BOOL)cheakIdentityCard: (NSString *)value { value = [value stringByTrimmingCharactersIn ...

  10. spring自动扫描、DispatcherServlet初始化流程、spring控制器Controller 过程剖析

    spring自动扫描1.自动扫描解析器ComponentScanBeanDefinitionParser,从doScan开始扫描解析指定包路径下的类注解信息并注册到工厂容器中. 2.进入后findCa ...