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. 在Linux中查看文件的编码及对文件进行编码转换

    如果你需要在Linux中操作windows下的文件,那么你可能会经常遇到文件编码转换的问题.Windows中默认的文件格式是GBK(gb2312),而Linux一般都是UTF-8.下面介绍一下,在Li ...

  2. 二模 (8) day1

    第一题: 题目大意: 梦幻城市每年为全市高中生兴办一次运动会.为促使各校同学之间的交流,采用特别的分队方式:每一个学校的同学,必须被均匀分散到各队,使得每一队中该校的人数皆相同.为增加比赛的竞争性,希 ...

  3. 三元运算+lambda表达式

    #三元运算,三目运算,if else简写 if 1 == 1: name = "liangml" else: name = "NB" #如果 1==1 成立,n ...

  4. CSS Hack及常用的技巧

    何谓CSS Hack? 不同的浏览器,比如Internet Explorer 6.Internet Explorer 7. Mozilla Firefox对CSS的解析认识不一样,因此会导致生成的页面 ...

  5. java---数据格式的验证

    package cc.cococ.trade.util; import java.util.regex.Matcher; import java.util.regex.Pattern; public ...

  6. iis提示“另一个程序正在使用此文件,进程无法访问。(异常来自HRESULT:0x80070020)

    看看IIS的网站,惊人的发现default web site是停止状态.印象中没有停止它啊.右键->管理网站->启动.点击启动后居然弹出:“另一个程序正在使用此文件,进程无法访问.(异常来 ...

  7. objective-c strong导致内存泄漏简单案例

    例如: @interface Test:NSObject{ id __strong obj_; } -(void) setObject:(id __strong)obj; @end @implemen ...

  8. UIButton 点击后变灰

    +(UIButton *)getBlueButtonWithTitle:(NSString *)aTitle{ UIButton *button = [UIButton buttonWithType: ...

  9. webview调用外部浏览器而不是在控件中显示

    view.loadUrl(url); // 如果页面中链接,如果希望点击链接继续在当前browser中响应,                     // 而不是新开Android的系统browser ...

  10. poj2955 区间dp

    //Accepted 200 KB 63 ms //区间dp //dp[i][j] 从i位到j位能得到的最大匹配数 //dp[i][j]=max(dp[i+1][j-1] (s[i-1]==s[j-1 ...