读书人

hdu4311 Meeting point-1 察看题

发布时间: 2012-09-14 11:53:44 作者: rapoo

hdu4311 Meeting point-1 观察题

Meeting point-1Problem DescriptionInputOutputSample InputSample Output
26202056HintIn 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;
}

读书人网 >编程

热点推荐