bzoj3170: [Tjoi2013]松鼠聚会

实际上这个距离就是切比雪夫距离

可以转换成曼哈顿距离,给出公式

(x1,y1)->(x2,y2)的切比雪夫距离==(x1+y1,x1-y1)->(x2+y2,x2-y2)的曼哈顿距离/2

那么曼哈顿就好搞了吧,直接一波前缀和

 

还有面积比是1:2(曼哈顿:切比雪夫)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;

struct node
{
    LL x,y;
}a[110000];
LL xx[110000],yy[110000];
LL sumx[110000],sumy[110000];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld%lld",&a[i].x,&a[i].y);
        a[i].x+=a[i].y;a[i].y=a[i].x-2*a[i].y;
        xx[i]=a[i].x;yy[i]=a[i].y;
    }
    
    sort(xx+1,xx+n+1);
    sumx[0]=0;for(int i=1;i<=n;i++)sumx[i]=sumx[i-1]+xx[i];
    sort(yy+1,yy+n+1);
    sumy[0]=0;for(int i=1;i<=n;i++)sumy[i]=sumy[i-1]+yy[i];
    
    LL ans=(1LL<<62);
    for(int i=1;i<=n;i++)
    {
        LL sum=0,id;
        
        id=lower_bound(xx+1,xx+n+1,a[i].x)-xx;
        sum+=((id-1)*xx[id]-sumx[id-1])+((sumx[n]-sumx[id])-(n-id)*xx[id]);
        
        id=lower_bound(yy+1,yy+n+1,a[i].y)-yy;
        sum+=((id-1)*yy[id]-sumy[id-1])+((sumy[n]-sumy[id])-(n-id)*yy[id]);
        
        ans=min(ans,sum);
    }
    printf("%lld\n",ans/2);
    return 0;
}

 

posted @ 2018-04-25 19:38  AKCqhzdy  阅读(153)  评论(0编辑  收藏  举报