Meeting point-1

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2866    Accepted Submission(s): 919

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)
题意:在一个二位坐标系中给出n个点的坐标,要求选择一个点作为中心,使其他点到该店的曼哈顿距离之和最小:
分析;首先把x和y坐标分离出来,分别从小到大排序,然后会形成一个绝对值之和,我们知道在某个点的左边求和去绝对值方式,和右边去绝对值求和方式,所以根据特性分别用sumx和sumy记录从左到右到达每个点的坐标和;
例如对于绝对值求和公式 :  
sum=|x+10|+|x+5|+|x+1|+|x-2|+|x-6|;sumx[1]=-10;sumx[2]=-15;sumx[3]=-16;sumx[4]=-14;sumx[5]=-8;  
对于第3个点sum=(-1)*3-sumx[3]+sum[5]-sum[3]-(-1)*(5-3);
然后现行枚举对于每个p[i]找出p[i].x和p[i].y在x[]和y[]数组中所对的下标套用公式即可:
程序:
#include"stdio.h"
#include"string.h"
#include"math.h"
#include"algorithm"
#include"vector"
#include"queue"
#include"stack"
#define M 100009
#define inf 1000000000000000LL
using namespace std;
struct node
{
__int64 x,y;
}p[M];
int n;
__int64 x[M],y[M],sumx[M],sumy[M];
int cmp(int a,int b)
{
return a<b;
}
int finde(__int64 *a,__int64 key)
{
int l=1,r=n,mid;
while(l<=r)
{
mid=(l+r)/2;
if(a[mid]==key)
return mid;
if(a[mid]>key)
r=mid-1;
else
l=mid+1;
}
return -1;
}
int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%I64d%I64d",&p[i].x,&p[i].y);
x[i]=p[i].x;
y[i]=p[i].y;
}
sort(x+1,x+n+1,cmp);
sort(y+1,y+n+1,cmp);
sumx[0]=sumy[0]=0;
for(i=1;i<=n;i++)
{
sumx[i]=sumx[i-1]+x[i];
sumy[i]=sumy[i-1]+y[i];
}
__int64 ans=inf;
for(i=1;i<=n;i++)
{
int dx=finde(x,p[i].x);
int dy=finde(y,p[i].y);
__int64 sx=x[dx]*dx-sumx[dx]+sumx[n]-sumx[dx]-(n-dx)*x[dx];
__int64 sy=y[dy]*dy-sumy[dy]+sumy[n]-sumy[dy]-(n-dy)*y[dy];
if(ans>sx+sy)
ans=sx+sy;
}
printf("%I64d\n",ans);
}
}

某个点到其他点的曼哈顿距离之和最小(HDU4311)的更多相关文章

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

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  2. hdu 4311 & 4312 Meeting point 曼哈顿距离之和最小

    hdu 4311 题意 平面上\(n(n\leq 1e5)\)个点,找一个点到其它所有点的曼哈顿距离之和最小. 思路 如果是找一个坐标使得所有点到其曼哈顿距离之和最小,那么将\(n\)个横坐标排个序, ...

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

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

  4. 51Nod 1108 距离之和最小 V2 1096 距离之和最小 中位数性质

    1108 距离之和最小 V2基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注三维空间上有N个点, 求一个点使它到这N个点的曼哈顿距离之和最小,输出这个最小 ...

  5. 51nod1110 距离之和最小 V3

    基准时间限制:1 秒 空间限制:131072 KB 分值: 40  X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].该点到其他点的带权距离 = 实际距离 * 权值.求X轴上 ...

  6. 51nod 1096 距离之和最小 1108 距离之和最小 V2

    [题解] 很显然在一条坐标轴上到各个点距离之和最小的点就是它们的中位数.怎么证明呢?我们假设现在找的某个点x左边有a个点,右边有b个点(a>b).我们把x向左移动d个单位,并保证x左边依然有a个 ...

  7. 1110 距离之和最小 V3

    1110 距离之和最小 V3 基准时间限制:1 秒 空间限制:131072 KB X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].该点到其他点的带权距离 = 实际距离 * ...

  8. 51Nod 1110 距离之和最小 V3 中位数 思维

    基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].点P到点P[i]的带权距离 = 实际距离 ...

  9. 【51NOD】1096 距离之和最小

    [算法]数学 [题解] 其实就是求中位数,奇数个点就是最中间的点,偶数个点就是最中间两个点和它们之间的区域皆可(所以偶数不必取到两点正中央,取两点任意一点即可). 我们可以想象现在x轴上有n个点,我们 ...

随机推荐

  1. SQL优化(zhuan)

    转自:http://www.jfox.info/SQL-you-hua 数据库的优化问题 一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出S ...

  2. ①spirngMVC框架运行原理图

  3. error: jump to label ‘XXXX’ [-fpermissive]

    http://www.cnblogs.com/foohack/p/4090124.html 下面的类似的源码在MSVC上能正确编译通过.但是gcc/g++上就会错: 1. if(expr)2. got ...

  4. QWidget 键盘事件 焦点(QApplication源码)

    在Qt中,键盘事件和QWidget的focus密不可分:一般来说,一个拥有焦点(focus)的QWidget或者grabKeyboard()的QWidget才可以接受键盘事件. 键盘事件派发给谁? 如 ...

  5. 【Android测试】【第一节】性能——CPU

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5065083.html 前言 本来打算写完全部的自动化测试之 ...

  6. Delphi FindowWindow,FindowWindowEx

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  7. mssql全文索引

    在使用全文索引的时候例如: SELECT [PRID] ,[PRCode] ,[PRDesc] FROM [test1].[dbo].[PerformanceIssue] where contains ...

  8. Java 实现导出excel表 POI

    1.首先下载poi-3.6-20091214.jar 2.Student.java import java.util.Date; public class Student { private int ...

  9. 20145211 《Java程序设计》第8周学习总结——自在飞花轻似梦

    教材学习内容总结 认识NIO Java NIO(New Input/Output)--新的输入/输出API包--是2002年引入到J2SE 1.4里的.Java NIO的目标是提高Java平台上的I/ ...

  10. WPF 本周、本月、本季、本年的第一天与最后一天取法

    lblWeekStart.Content = DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOf ...