【BZOJ】【P3210】【花神的浇花集会】【题解】【曼哈顿距离变形】

传送门: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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值