设为首页 加入收藏

TOP

hdu4311 曼哈顿距离
2015-07-20 17:24:06 来源: 作者: 【 】 浏览:2
Tags:hdu4311 曼哈顿 距离

http://acm.hdu.edu.cn/showproblem.php?pid=4311

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 For each test case, the first line is an integer n represents there are n retired TJU-ACMers. (0
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)
/**
hdu 4311 曼哈顿距离 
题目大意:在给定的n个点中选择一个点,使得其他点到这个点的曼哈顿距离之和最小,求出这个最小的距离
解题思路:如果我们确定了这个点的坐标为 (x,y).xx为所有点的横坐标之和,numlx表示该点左边的点的个数,
          那么lengx=(x*numlx-sumx[1~numlx-1])+(sumx[numlx~n]-x*(n-numlx))=x*(2*numlx-n)+xx-2*sum[1~numlx];
          对于纵坐标的处理类似。
          这些工作做好之后我们把n个点都枚举1遍取最小就可以了。
*/
#include 
    
     
#include 
     
       #include 
      
        #include 
       
         using namespace std; typedef long long LL; const int maxn=100005; struct note { int x,y,id; } a[maxn]; int x[maxn],y[maxn],n; LL numx[maxn],numy[maxn]; bool cmp1(note a,note b) { return a.x
        
         

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu3709 Balanced Number 数位dp 下一篇hdu 3853 LOOPS DP

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python 数据分析与可 (2025-12-26 21:51:20)
·从零开始学Python之 (2025-12-26 21:51:17)
·超长干货:Python实 (2025-12-26 21:51:14)
·为什么 Java 社区至 (2025-12-26 21:19:10)
·Java多线程阻塞队列 (2025-12-26 21:19:07)