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. Redis系列-存储篇string主要操作函数小结

    通过上两篇的介绍,我们的redis服务器基本跑起来.db都具有最基本的CRUD功能,我们沿着这个脉络,开始学习redis丰富的数据结构之旅,当然先从最简单且常用的string开始. 1.新增 a)se ...

  2. win7 MS SQL SERVER 2000安装

    http://blog.chinaunix.net/uid-24398518-id-2156226.html MicrosoftInternetExplorer402DocumentNotSpecif ...

  3. 【C语言学习】-02 分支结构

    本文目录: 一.BOOL布尔类型 二.关系运算符 三.逻辑运算符 四.if语句 五.枚举类型 六.switch语句 一.BOOL布尔类型 BOOL数据类型,是一种表示非真即假的数据类型,布尔类型的变量 ...

  4. hihocoder 1236(2015北京网络赛 J题) 分块bitset乱搞题

    题目大意: 每个人有五门课成绩,初始给定一部分学生的成绩,然后每次询问给出一个学生的成绩,希望知道在给定的一堆学生的成绩比这个学生每门都低或者相等的人数 因为强行要求在线查询,所以题目要求,每次当前给 ...

  5. C++全局变量在多个源代码文件中的使用

    在比较大的项目中,如果需要使用全局变量,那么就需要注意一些全局变量声明.使用不当引起的问题了. 本篇文章主要内容有两个:普通全局变量.静态全局变量.全局常量. 1.普通全局变量:假设我们需要在多个不同 ...

  6. centos ssh 无密码登录

    在linux系统中,ssh是远程登录的默认工具,因为该工具的协议使用了RSA/DSA的加密算法.该工具做linux系统的远程管理是非常安全的.telnet,因为其不安全性,在linux系统中被搁置使用 ...

  7. STM32之延时秒,毫秒,微秒

    #include "delay.h" #include "stdint.h" #include "stm32f10x.h" ; //us延时 ...

  8. 提交自己的插件包(package)

    安装 把下列命令粘贴到终端上: mkdir -p ~/Library/Application\ Support/Developer/Shared/Xcode/Plug-ins; curl -L htt ...

  9. IIS使用问题

    1.System.Data.SQLite”或它的某一个依赖项.试图加载格式不正确的程序:修改IIS应用程序池的高级设置将32位设置成true

  10. oracle 删除表和数据分析语句

    TRUNCATE TABLE 表名;ANALYZE TABLE 表名 ESTIMATE STATISTICS;