HDU 4311 Meeting point-1

来源:互联网 发布:用淘宝怎么贷款 编辑:程序博客网 时间:2024/04/27 09:06


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<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
46-4 -1-1 -22 -40 20 35 -260 02 0-5 -22 -2-1 24 05-5 1-1 33 13 -11 -110-1 -1-3 2-4 45 25 -43 -14 3-1 -23 4-2 2
 

Sample Output
26202056
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)
 

比赛的时候脑子秀逗了。。。。弱智到数学化简都不会了啊~~~~

之所以放上来是为了鄙视自己一番,顺便提醒一下WA的地方。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>using namespace std;const int maxn=100010;struct in{    int x,y,w;}a[maxn];__int64 b[maxn],c[maxn],sum1[maxn],sum2[maxn],sum[maxn],p[maxn],q[maxn];bool cmp1(in a,in b){    return a.x<b.x;}bool cmp2(in a,in b){    return a.y<b.y;}int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        memset(sum1,0,sizeof(sum1));        memset(sum,0,sizeof(sum));        memset(sum2,0,sizeof(sum2));memset(p,0,sizeof(p));        memset(q,0,sizeof(q));        memset(a,0,sizeof(a));        scanf("%d",&n);        for(int i=1;i<=n;i++)        {            scanf("%d%d",&a[i].x,&a[i].y);            a[i].w=i;        }        sort(a+1,a+n+1,cmp1);        p[0]=0;        for(int i=1;i<=n;i++)        {            p[i]+=a[i].x+p[i-1];        }        for(int i=1;i<=n;i++)        {        __int64 ks=a[i].x;        ks*=(i-1);//相乘的时候会爆int,需要单独拿出来,自乘的办法         __int64 kk=a[i].x;        kk*=(n-i);            sum1[a[i].w]=ks-(p[i-1])+(p[n]-p[i])-kk;        }        sort(a+1,a+n+1,cmp2);        q[0]=0;        for(int i=1;i<=n;i++)        {            q[i]+=a[i].y+q[i-1];        }        for(int i=1;i<=n;i++)        {        __int64 ks2=a[i].y;        ks2*=(i-1);        __int64 kk2=a[i].y;        kk2*=(n-i);            sum2[a[i].w]=ks2-(q[i-1])+(q[n]-q[i])-kk2;        }        for(int i=1;i<=n;i++)        {            sum[a[i].w]=sum1[a[i].w]+sum2[a[i].w];        }        sort(sum+1,sum+n+1);        printf("%I64d\n",sum[1]);        //cout<<endl;    }    return 0;}