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人在不同的地方,选择其中一个人的屋子,使得其他人到这个地方去的距离之和最短
 
题解:题目要求的距离是曼哈顿距离|x1-x2|+|y1-y2|;所以可以考虑将x,y,分开计算,最后取和的max;
 如何快速处理其他位置到i位置的距离dis[i]?先将坐标排个序 记录前缀和
 可以发现相邻位置的两个i,j=i+1的距离和的关系
dis[j]=dis[i]+(j-2)*(sumx[j]-sumx[i])-(n-j)*(sumx[j]-sumx[i]) =dis[i]+(2*i-n)*(sumx[j]-sumx[i])
 
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<cmath>
#define ll long long
#define PI acos(-1.0)
#define mod 1000000007
int t;
using namespace std;
struct node
{
ll x,y,pos;
} N[];
ll sumx[];
ll sumy[];
ll ans1[];
ll ans2[];
int exm;
bool cmp1(struct node aa,struct node bb)
{
return aa.x<bb.x;
}
bool cmp2(struct node aa,struct node bb)
{
return aa.y<bb.y;
}
int main()
{
while(~scanf("%d",&t))
{
for(int i=; i<=t; i++)
{
scanf("%d",&exm);
memset(N,,sizeof(N));
memset(ans1,,sizeof(ans1));
memset(ans2,,sizeof(ans2));
for(int j=; j<=exm; j++)
{
scanf("%I64d %I64d",&N[j].x,&N[j].y);
N[j].pos=j;
}
sort(N+,N++exm,cmp1);
sumx[]=;
for(int j=; j<=exm; j++)
{
sumx[j]=sumx[j-]+N[j].x-N[j-].x;
ans1[N[].pos]+=sumx[j];
}
for(int j=; j<=exm; j++)
{ ans1[N[j].pos]=ans1[N[j-].pos]-(exm-j)*(sumx[j]-sumx[j-])+(j-)*(sumx[j]-sumx[j-]);
}
sort(N+,N++exm,cmp2);
sumy[]=;
for(int j=; j<=exm; j++)
{
sumy[j]=sumy[j-]+N[j].y-N[j-].y;
ans2[N[].pos]+=sumy[j];
}
for(int j=; j<=exm; j++)
{
ans2[N[j].pos]=ans2[N[j-].pos]-(exm-j)*(sumy[j]-sumy[j-])+(j-)*(sumy[j]-sumy[j-]);
}
ll maxn=ans1[]+ans2[];
for(int j=; j<=exm; j++)
{
if(ans1[j]+ans2[j]<maxn)
{
maxn=ans1[j]+ans2[j];
}
}
printf("%I64d\n",maxn);
}
}
return ;
}

HDU 4311 前缀和的更多相关文章

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

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

  2. 【HDU 4311】Meeting point-1(前缀和求曼哈顿距离和)

    题目链接 正经解法: 给定n个点的坐标,找一个点,到其他点的曼哈顿距离之和最小.n可以是100000.大概要一个O(nlogn)的算法.算曼哈顿距离可以把x和y分开计算排好序后计算前缀和就可以在O(1 ...

  3. HDU 4311 Meeting point-1(曼哈顿距离最小)

    http://acm.hdu.edu.cn/showproblem.php?pid=4311 题意:在二维坐标中有n个点,现在要从这n个点中选出一个点,使得其他点到该点的曼哈顿距离总和最小. 思路: ...

  4. hdu 5084 前缀和预处理

    http://acm.hdu.edu.cn/showproblem.php?pid=5084 给出矩阵M,求M*M矩阵的r行c列的数,每个查询跟前一个查询的结果有关. 观察该矩阵得知,令ans = M ...

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

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

  6. HDU 4311 Meeting point-1 && HDU 4312 Meeting point-2

    这俩个题  题意::给出N(<1e5)个点求找到一个点作为聚会的地方,使每个点到达这里的距离最小.4311是 曼哈顿距离 4312是 切比雪夫距离: 曼哈顿距离 :大家都知道 对于二维坐标系a( ...

  7. HDU - 6186 前缀和位运算

    异或操作蒙蔽了我的双眼 以至于没有第一时间想到前缀和与后缀和 水题做的不够多 #include<bits/stdc++.h> #define rep(i,j,k) for(register ...

  8. hdu 5163(前缀和+分类讨论)

    Taking Bus Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. hdu 6025 前缀 后缀 gcd

    大致题意: 去掉一个元素能使这个数列的GCD最大为多少 分析: 我们求一个数列的GCD,是先求前两个元素的GCD,然后将这个GCD值在与下一个元素进行GCD运算.由此可知进行GCD运算的顺序对最终的结 ...

随机推荐

  1. 从ajax获取的数据无法通过Jquery选择器来调用事件

    如果标签是动态生成的,比如说div.tr.td等,若需通过Jquery来获取事件,那么需要用live来绑定相应的事件. 比如说绑定div的click事件 $("div").live ...

  2. (DFS)zoj1008-Gnome Tetravex

    现在zoj暂时关了,实际上是在scuoj上做的. 题目地址 看起来题目比较复杂,实际上主要需要思维的是如何恰当的剪枝及合适的DFS角度. 问题等价于将n*n个可能相同的方块放到一个n*n的表中,使满足 ...

  3. RM报表的打印偏移

    自己摸索一下 RMReport1.SaveReportOptions.AutoLoadSaveSetting := True; RMReport1.SaveReportOptions.UseRegis ...

  4. 一步步学习JSON

    什么是Json json是JavaScript Object Notation(javascript对象表示法)的缩写,是一种轻量的数据格式,是基于javascript的一个子集.与XML一样,jso ...

  5. [USACO精选] 第一章 数值计算

    好不容易坑来了传说中的USACO精选,近100题我要是能做完就哈哈哈哈了…继今天学并查集连番受挫之后,决定写一写基础题. #0 负二进制 2014-01-10 其实是想到就会做,不想到就不会做的题,数 ...

  6. 中南民航如何利用K2BPM构建业务流程?

    广州市中南民航空管通信网络科技有限公司成立于2004年9月,负责民航中南地区通信网络.电报网络的建设与维护.民航空管信息系统的开发与维护.公司拥有一支技术精湛的研发队伍,在信息技术领域领跑于民航空管行 ...

  7. DB2配置信息查看及其更新命令

    获取DB2配置信息 db2 get dbm cfg 更新DB2链接配置信息 db2 update dbm cfg using authentication server db2stop db2star ...

  8. python解无忧公主的数学时间097.py

    python解无忧公主的数学时间097.py """ python解无忧公主的数学时间097.py codegay 2016年3月30日 00:17:26 http:// ...

  9. 关于resolve非泛型方法不能与类型实参一起使用

    今天mvc新建三层时,写到bll层中一直报下面的错误,检查了几遍赶脚并没有什么错.最后发现缺少一些引用. 如下面的图,少添加了下面的两个引用.Unity是微软模式与实践团队开发的一个轻量级.可扩展的依 ...

  10. Hadoop MRUnit使用(一)

    之前在写MR job的时候,由于要在云梯,或者一淘的开发集群上运行:所以处理方法是,在本地打成jar包,然后scp到客户端网关机上,然后在提交job运行.这样的问题时,有时候如果遇到一些逻辑上的问题, ...