AtCoder Beginner Contest 178 E - Dist Max 题解(推公式)
题目链接
题目大意
给你n个点(n<=2e5)要你求所有点中两个点最短的曼哈顿距离
曼哈顿距离定义为d(i,j)=|x1-x2|+|y1-y2|.
题目思路
想了很久也没有什么思路,其实就是一个简单的推公式
x1-x2与y1-y2如果同号,那么d(i,j)=abs(x1-x2+y1-y2)=abs(x1+y1-(x2+y2))
x1-x2与y1-y2如果异号,那么d(i,j)=abs(x1-x2-y1+y2)=abs(x1 - y1-(x2-y2))
那么答案就是取max(max(|xi+yi|)-min(|xj+yj|),max(|xi-yi|)-min(|xj-yj|))
代码
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
#define fi first
#define se second
#define debug printf(" I am here\n");
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int maxn=1000+5,inf=0x3f3f3f3f;
const double eps=1e-10;
int n;
signed main(){
scanf("%d",&n);
int ma1=-inf,mi1=inf,ma2=-inf,mi2=inf;
for(int i=1,x,y;i<=n;i++){
scanf("%d%d",&x,&y);
ma1=max(ma1,x-y);
mi1=min(mi1,x-y);
ma2=max(ma2,x+y);
mi2=min(mi2,x+y);
}
printf("%d\n",max(ma1-mi1,ma2-mi2));
return 0;
}
不摆烂了,写题