码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4311 前缀和

时间:2016-08-01 19:21:34      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

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])
 
 1 /******************************
 2 code by drizzle
 3 blog: www.cnblogs.com/hsd-/
 4 ^ ^    ^ ^
 5  O      O
 6 ******************************/
 7 //#include<bits/stdc++.h>
 8 #include<iostream>
 9 #include<cstring>
10 #include<cstdio>
11 #include<map>
12 #include<algorithm>
13 #include<cmath>
14 #define ll long long
15 #define PI acos(-1.0)
16 #define mod 1000000007
17 int t;
18 using namespace std;
19 struct node
20 {
21     ll x,y,pos;
22 } N[100005];
23 ll sumx[100005];
24 ll sumy[100005];
25 ll ans1[100005];
26 ll ans2[100005];
27 int exm;
28 bool cmp1(struct node aa,struct node bb)
29 {
30     return aa.x<bb.x;
31 }
32 bool cmp2(struct node aa,struct node bb)
33 {
34     return aa.y<bb.y;
35 }
36 int main()
37 {
38     while(~scanf("%d",&t))
39     {
40         for(int i=1; i<=t; i++)
41         {
42             scanf("%d",&exm);
43             memset(N,0,sizeof(N));
44             memset(ans1,0,sizeof(ans1));
45             memset(ans2,0,sizeof(ans2));
46             for(int j=1; j<=exm; j++)
47             {
48                 scanf("%I64d %I64d",&N[j].x,&N[j].y);
49                 N[j].pos=j;
50             }
51             sort(N+1,N+1+exm,cmp1);
52             sumx[1]=0;
53             for(int j=2; j<=exm; j++)
54             {
55                 sumx[j]=sumx[j-1]+N[j].x-N[j-1].x;
56                 ans1[N[1].pos]+=sumx[j];
57             }
58             for(int j=2; j<=exm; j++)
59             {
60 
61                 ans1[N[j].pos]=ans1[N[j-1].pos]-(exm-j)*(sumx[j]-sumx[j-1])+(j-2)*(sumx[j]-sumx[j-1]);
62             }
63             sort(N+1,N+1+exm,cmp2);
64             sumy[1]=0;
65             for(int j=2; j<=exm; j++)
66             {
67                 sumy[j]=sumy[j-1]+N[j].y-N[j-1].y;
68                 ans2[N[1].pos]+=sumy[j];
69             }
70             for(int j=2; j<=exm; j++)
71             {
72                 ans2[N[j].pos]=ans2[N[j-1].pos]-(exm-j)*(sumy[j]-sumy[j-1])+(j-2)*(sumy[j]-sumy[j-1]);
73             }
74             ll maxn=ans1[1]+ans2[1];
75             for(int j=1; j<=exm; j++)
76             {
77                 if(ans1[j]+ans2[j]<maxn)
78                 {
79                     maxn=ans1[j]+ans2[j];
80                 }
81             }
82             printf("%I64d\n",maxn);
83         }
84     }
85     return 0;
86 }

 

HDU 4311 前缀和

标签:

原文地址:http://www.cnblogs.com/hsd-/p/5726546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!