HDU 4311——Meeting point-1(暴力)
来源:互联网 发布:算法导论中算法 编辑:程序博客网 时间:2024/04/20 16:05
题目:
A - Meeting point-1
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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.
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)
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
46-4 -1-1 -22 -40 20 35 -260 02 0-5 -22 -2-1 24 05-5 1-1 33 13 -11 -110-1 -1-3 2-4 45 25 -43 -14 3-1 -23 4-2 2
Sample Output
26202056
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)
题意:有很多人在不同的地方,选择其中一个人的屋子,使得其他人到这个地方去的距离最短,这里的距离是指x轴和y轴方向加起来。而不是欧几里得距离。
思路:分别求出每个人在x方向上和y方向上离其他人距离的总和。计算的时候的技巧就是先排序,第i个人和第i-1个人相差的距离和就是(n-2*i)*(Xi-Xi-1),这个很容易推得。这样就是O(n)的算法了。
代码如下:
<pre name="code" class="cpp">#include <iostream>#include <cmath>#include <cstring>#include <cstdio>#include <vector>#include <string>#include <algorithm>#include <string>using namespace std;#define MAXN 100010#define INF 1e9+7#define MODE 1000000struct pos{ long long x; long long y; int ID;};pos a[MAXN];long long dx[MAXN];long long dy[MAXN];int cmp1(pos a,pos b){ if(a.x!=b.x) return a.x<b.x; else return a.y<b.y;}int cmp2(pos a,pos b){ if(a.y!=b.y) return a.y<b.y; else return a.x<b.x;}//int x[MAXN];//int y[MAXN];////long long solve(int a[],int n)//{// sort(a,a+n);// int x;// if(n%2==0)// {// x=n/2;// }// else// x=n/2;// long long res=0;// for(int i=0;i<n;i++)// res+=abs(a[i]-a[x])+abs(a[i]);// return res;//}int main(){ //freopen("in.txt","r",stdin); int t,n; scanf("%d",&t); while(t--) { scanf("%d",&n); long long minx=0,miny=0,minn=INF; for(int i=0;i<n;i++){ scanf("%I64d%I64d",&a[i].x,&a[i].y); a[i].ID=i; } sort(a,a+n,cmp1); for(int i=0;i<n;i++) { minx+=abs(a[i].x-a[0].x); } dx[a[0].ID]=minx; for(int x=1;x<n;x++) { dx[a[x].ID]=dx[a[x-1].ID]-(a[x].x-a[x-1].x)*(n-2*x); } sort(a,a+n,cmp2); for(int i=0;i<n;i++) { miny+=abs(a[i].y-a[0].y); } dy[a[0].ID]=miny; for(int x=1;x<n;x++) { dy[a[x].ID]=dy[a[x-1].ID]-(a[x].y-a[x-1].y)*(n-2*x); } minn=dx[0]+dy[0]; for(int i=1;i<n;i++) { minn=min(minn,dx[i]+dy[i]); } printf("%I64d\n",minn); }}
0 0
- HDU 4311——Meeting point-1(暴力)
- HDU 4311 Meeting point-1
- hdu 4311 Meeting point-1
- hdu 4311 Meeting point-1
- HDU 4311 Meeting point-1
- HDU 4311Meeting point-1
- hdu 4311 Meeting point-1 #manhattan距离
- hdu - 4311 - Meeting point-1 - 想法题
- hdu 4311 Meeting point-1(3级)
- hdu 4311 - Meeting point-1(预处理)
- HDU 4311 Meeting point-1 简单几何
- HDU 4311 Meeting point-1 (思维)
- HDU 4311 Meeting point-1 && HDU 4312 Meeting point-2 曼哈顿距离 与 切比雪夫距离
- HDOJ-4311 Meeting point-1
- HDU 4311&2 Meeting point-1&2(曼哈顿距离&&切比雪夫距离)
- hdu 4311 Meeting point-1 递推 多校联合赛(二) 第二题
- hdu 4311 Meeting point-1 (快速求解曼哈顿距离和)
- hdu 4311 Meeting point-1 递推 多校联合赛(二) 第二题 好题
- js中 string 转 int 注意的问题——parseInt
- AndroidStudio导入项目时卡在Building gradle project info的解决方法
- 7.18 图的相关知识
- Java连接oracle数据库
- php安装redis扩展
- HDU 4311——Meeting point-1(暴力)
- 257. Binary Tree Paths
- 封装缓动框架
- 关于<<STL源码剖析>>中4.4.5节测试程序不能通过编译的说明
- STOMP Over WebSocket(滑稽的翻译成跺脚吧)
- CodeForces 589A Email Aliases
- 【Linux C】浅谈指针(二)——野指针
- shell学习
- 插入排序