题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3170

题意:给出二维平面上n个点 (xi,yi)。求一点t(1<=t<=n)使得下面的式子最小:

BZOJ 3170 松鼠聚会(XY坐标)-LMLPHP

思路:

BZOJ 3170 松鼠聚会(XY坐标)-LMLPHP

struct node
{
    int x,y;
    
    node(){}
    node(int _x,int _y)
    {
        x=_x;
        y=_y;
    }
};

node a[N],b[N],c[N];
int n;
i64 p[N],q[N];

int cmp(node a,node b)
{
    return a.x<b.x;
}

int main()
{
    RD(n);
    int i;
    FOR1(i,n)
    {
        RD(a[i].x,a[i].y);
        b[i]=node(a[i].x+a[i].y,i);
        c[i]=node(a[i].x-a[i].y,i);
    }    
    sort(b+1,b+n+1,cmp);
    sort(c+1,c+n+1,cmp);
    i64 sumX=0,sumY=0;
    FOR1(i,n) sumX+=b[i].x,sumY+=c[i].x;
    i64 preX=0,preY=0;
    FOR1(i,n)
    {
        p[b[i].y]=(i64)b[i].x*(i-1)-preX+sumX-preX-b[i].x-(i64)b[i].x*(n-i);
        q[c[i].y]=(i64)c[i].x*(i-1)-preY+sumY-preY-c[i].x-(i64)c[i].x*(n-i);
        preX+=b[i].x;
        preY+=c[i].x;
    }
    i64 ans=inf;
    FOR1(i,n) upMin(ans,p[i]+q[i]);
    PR(ans>>1);
}

05-11 11:03