传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3210
|x_1-x_2|+|y_1-y_2|=max(|(x_1+y_1)-(x_2+y_2)|,(x_1-y_1)-(x_2-y_2))
Code:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+5;
int n,a[maxn],b[maxn],d[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,-1},{-1,1}};
double x[maxn],y[maxn];
typedef long long LL;
LL calc(int x,int y){
LL ans=0;
for(int i=1;i<=n;i++)
ans+=max(abs(x-a[i]),abs(y-b[i]));
return ans;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i],&b[i]);
x[i]=0.5*a[i]+0.5*b[i];
y[i]=0.5*a[i]-0.5*b[i];
}sort(x+1,x+1+n);sort(y+1,y+1+n);
double mx=x[(n+1)/2],my=y[(n+1)/2];
double mx2=mx+my,my2=mx-my;
LL ans=calc(int(mx2),int(my2));
for(int i=0;i<8;i++)ans=min(ans,calc(trunc(mx2+d[i][0]),trunc(my2+d[i][1])));
cout<<ans<<endl;
return 0;
}