您的位置:首页 > 其它

hdu 4311 Meeting point-1(3级)

2013-06-01 09:10 190 查看

Meeting point-1

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2313 Accepted Submission(s): 731



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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)

[align=left]Output[/align]
For each test case, output the minimal sum of travel times.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

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)


[align=left]Author[/align]
TJU

[align=left]Source[/align]
2012 Multi-University Training Contest 2

[align=left]Recommend[/align]
zhuyuanchen520

思路:x,y到某点的曼哈顿距离可以分开算,因此,分开枚举以某点为中心的值球最小就好了。

预处理,kx[x]以某点 x为中心的所有x距离和,ky[]同理。kx[i]=kx[i-1]+(i-n+i)*(f[i].x-f[i-1].x);

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int mm=2e5+9;
class node
{
public:
__int64 x,y;
int id;
} f[mm];
bool cmpx(node a,node b)
{
return a.x<b.x;
}
bool cmpy(node a,node b)
{
return a.y<b.y;
}
__int64 kx[mm],ky[mm];
__int64 sum;
__int64 aabs(__int64 x)
{
if(x<0)return -x;
return x;
}
int main()
{
int cas;__int64 n;
while(~scanf("%d",&cas))
{
while(cas--)
{
scanf("%I64d",&n);
for(int i=0; i<n; ++i)
scanf("%I64d%I64d",&f[i].x,&f[i].y);
sort(f,f+n,cmpx);
kx[0]=0;
for(int i=0; i<n; ++i)
{
kx[0]+=aabs(f[i].x-f[0].x);
f[i].id=i;
}
for(__int64 i=1; i<n; ++i)
kx[i]=kx[i-1]+(i-n+i)*(f[i].x-f[i-1].x);
//for(int i=0;i<n;++i)
//cout<<" "<<kx[i]<<" ";puts("");
sort(f,f+n,cmpy);
ky[0]=0;
for(int i=0;i<n;++i)
ky[0]+=aabs(f[i].y-f[0].y);
for(__int64 i=1;i<n;++i)
ky[i]=ky[i-1]+(i+i-n)*(f[i].y-f[i-1].y);
//for(int i=0;i<n ;++i)
//cout<<" "<<ky[i]<<" ";puts("");
sum=6e18;
for(int i=0;i<n;++i)
{ //cout<<f[i].id<<" "<<i<<" "<<kx[f[i].id]<<" "<<ky[i]<<" "<<ky[i]+kx[f[i].id]<<endl;
if(sum>ky[i]+kx[f[i].id])sum=ky[i]+kx[f[i].id];
}
printf("%I64d\n",sum);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: