首页 > 代码库 > BZOJ 3170 松鼠聚会

BZOJ 3170 松鼠聚会

日常刷水。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#define maxn 100500
#define inf 0x7f7f7f7f7f7f7f7fLL
using namespace std;
long long n,idx[maxn],idy[maxn],xs,ys,sumx[maxn],sumy[maxn],ans=inf;
struct data
{
    long long val;
    long long id;
    data (long long val,long long id):val(val),id(id) {}
    data () {}
}x[maxn],y[maxn];
bool cmp(data x,data y)
{
    return x.val<y.val;
}
int main()
{
    scanf("%lld",&n);
    for (long long i=1;i<=n;i++)
    {
        scanf("%lld%lld",&xs,&ys);
        x[i]=data(xs+ys,i);y[i]=data(xs-ys,i);
    }
    sort(x+1,x+n+1,cmp);sort(y+1,y+n+1,cmp);
    for (long long i=1;i<=n;i++) idx[x[i].id]=i,idy[y[i].id]=i;
    for (long long i=1;i<=n;i++) sumx[i]=sumx[i-1]+x[i].val,sumy[i]=sumy[i-1]+y[i].val;
    for (long long i=1;i<=n;i++)
    {
        long long k1=idx[i],k2=idy[i];
        ans=min(ans,sumx[n]-2*sumx[k1]+2*k1*x[k1].val-n*x[k1].val + sumy[n]-2*sumy[k2]+2*k2*y[k2].val-n*y[k2].val);
    }
    printf("%lld\n",ans/2);
    return 0;
}

 

BZOJ 3170 松鼠聚会