hdu4311 Meeting point-1 察看题
hdu4311 Meeting point-1 观察题
#include<cstring>
#include<algorithm>
#define MAXN 100010
using namespace std;
struct point{long long x,y;}a[MAXN];
bool cmp(point A,point B)
{
if(A.x<B.x) return true;
if(A.x==B.x&&A.y<B.y) return true;
return false;
}
long long ABS(long long A)
{
if(A<0) A=-A;
return A;
}
long long work(int k,int n)
{
int i;
long long ans=0;
for(i=0;i<n;i++)
{
ans+=ABS(a[k].x-a[i].x);
ans+=ABS(a[k].y-a[i].y);
}
return ans;
}
int main()
{
int test,n,s,e,i;
long long ans,t;
scanf("%d",&test);
while(test--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%I64d%I64d",&a[i].x,&a[i].y);
}
sort(a,a+n,cmp);
s=n/2-250;e=n/2+250;
if(s<0) s=0;
if(e>n-1) e=n-1;
ans=1;ans<<=60;
for(i=s;i<=e;i++)
{
t=work(i,n);
if(ans>t) ans=t;
}
cout<<ans<<endl;
}
return 0;
}
Meeting point-1
Problem DescriptionIt 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
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 56HintIn 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排序 从中间往2边找250个 就可以了 就能找到那个点
以后要是出不了题 就大胆的试试
#include<iostream>
#include<cstdio>#include<cstring>
#include<algorithm>
#define MAXN 100010
using namespace std;
struct point{long long x,y;}a[MAXN];
bool cmp(point A,point B)
{
if(A.x<B.x) return true;
if(A.x==B.x&&A.y<B.y) return true;
return false;
}
long long ABS(long long A)
{
if(A<0) A=-A;
return A;
}
long long work(int k,int n)
{
int i;
long long ans=0;
for(i=0;i<n;i++)
{
ans+=ABS(a[k].x-a[i].x);
ans+=ABS(a[k].y-a[i].y);
}
return ans;
}
int main()
{
int test,n,s,e,i;
long long ans,t;
scanf("%d",&test);
while(test--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%I64d%I64d",&a[i].x,&a[i].y);
}
sort(a,a+n,cmp);
s=n/2-250;e=n/2+250;
if(s<0) s=0;
if(e>n-1) e=n-1;
ans=1;ans<<=60;
for(i=s;i<=e;i++)
{
t=work(i,n);
if(ans>t) ans=t;
}
cout<<ans<<endl;
}
return 0;
}