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. dom paser

    dom paser /** * */ package ec.utils; import java.io.BufferedInputStream; import java.io.ByteArrayInp ...

  2. 打造私有的DNS 服务

    在企业内部一般都会有自己的DNS服务,主要为了企业方便管理,例如访问一些内部系统,控制一些网站不让访问,今天我们就说说如何打造私有的DNS服务 系统基础配置 Ubuntu14.04  内存:354M ...

  3. URL编码数据转换为JSON数据

    NSString *urlString; urlString=[self    URLDecodedString:urlString]; -(NSString *)URLDecodedString:( ...

  4. QOS

    QoS控制原理 · 基于设备的QoS保障技术——CAC· 基于优先级设置的QoS保障技术· 基于减小抖动的QoS保障-Jitter BufferIP传输在时延.抖动.丢包率方面的性能不及TDM传输,而 ...

  5. TCP粘包

    一.通信协议TCP/UDP: TCP(transport control protocol,传输控制协议)是面向连接的,面向流的,提供高可靠性服务.客户端和服务器端都要有一一成对的socket, 因此 ...

  6. JSON格式解析和libjson使用简介(关于cjson的使用示例)

    JSON格式解析和libjson使用简介 在阅读本文之前,请先阅读下<Rss Reader实例开发之系统设计>一文. Rss Reader实例开发中,进行网络数据交换时主要使用到了两种数据 ...

  7. storyboard中xib文件不加载问题

    今天在用Xcode6自定义视图控制器时附带了一个XIB文件,然后把自定义的类绑定到storyboard的ViewController,如图所示  , 发现RootViewController对应的xi ...

  8. ecshop前台英文后台中文

    方法一:修改init.php文件方法改变ecshop默认语言包 该方法适用于:安装ecshop中文版的站长使用. 1.进入您的后台,按照下图提示,将系统语言选项设为 en_us(英语) ,确定保存. ...

  9. 《JAVA NIO》Channel

    3.通道 Channle主要分为两类:File操作对应的FIleChannel和Stream操作对应的socket的3个channe. 1.这3个channel都是抽象类.其具体实现在SPI里面. 2 ...

  10. http://tedhacker.top/2016/08/05/Spring%E7%BA%BF%E7%A8%8B%E6%B1%A0%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95/

    http://tedhacker.top/2016/08/05/Spring%E7%BA%BF%E7%A8%8B%E6%B1%A0%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%9 ...