bzoj3210: 花神的浇花集会

第二次遇到切比雪夫了

那么大力转成曼哈顿求中位数

然而这题有坑点,求出那个曼哈顿的坐标还原成切比雪夫可能不是整点

那么就把它四周的点都算一次取min就好

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

int n,X[110000],Y[110000];
LL getsum(int mx,int my)
{
    LL sum=0;
    for(int i=1;i<=n;i++)
    {
        sum+=(LL(abs(mx-X[i])))+(LL(abs(my-Y[i])));
    }
    return sum;
}
int main()
{
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&X[i],&Y[i]);
        X[i]+=Y[i];Y[i]=X[i]-2*Y[i];
    }
    int mx,my;
    sort(X+1,X+n+1);mx=X[n/2+1];
    sort(Y+1,Y+n+1);my=Y[n/2+1];
    
    if((mx+my)%2==1)
        printf("%d\n",min(min(getsum(mx-1,my),getsum(mx,my-1)),min(getsum(mx,my+1),getsum(mx+1,my)))/2);
    else 
        printf("%d\n",getsum(mx,my)/2);
    return 0;
}

 

posted @ 2018-09-19 11:54  AKCqhzdy  阅读(214)  评论(0编辑  收藏  举报