题目大意:给出若干个农场的坐标,求出相距最远的农场的直线距离的平方。
题解:
首先扫描法求出凸包,旋转卡壳求出最大值即可
代码:
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;typedef long long ll;
const int maxn=50000+100;
const int INF=0x3f3f3f3f;struct Node{int x,y;
}node[maxn],tubao[maxn];
int top;
int n;inline ll cross(Node a,Node b,Node c){return (ll)(b.x-a.x)*(c.y-a.y)-(ll)(c.x-a.x)*(b.y-a.y);
} inline bool cmp(Node a,Node b){if(cross(node[1],a,b)==0) return a.x<b.x;return cross(node[1],a,b)>0;
}void getTuBao(){node[0]=(Node){INF,INF};int tmp;for(int i=1;i<=n;i++){if(node[i].y<node[0].y || (node[i].y==node[0].y && node[i].x<node[0].x)) node[0]=node[i],tmp=i;}swap(node[1],node[tmp]);sort(node+2,node+1+n,cmp);tubao[0]=node[1];tubao[1]=node[2];top=1;for(int i=3;i<=n;i++){while(top&&cross(tubao[top-1],tubao[top],node[i])<=0) top--;tubao[++top]=node[i];}
} ll Dis(Node a,Node b){return (ll)(a.x-b.x)*(a.x-b.x)+(ll)(a.y-b.y)*(a.y-b.y);
}void getMaxDis(){ll dis=0;if(top==1) dis=Dis(tubao[0],tubao[1]);else{tubao[++top]=tubao[0];int j=2;for(int i=0;i<top;i++){while(cross(tubao[i],tubao[i+1],tubao[j])<cross(tubao[i],tubao[i+1],tubao[j+1])){j++;if(j==top) j=0;}dis=max(dis,max(Dis(tubao[i],tubao[j]),Dis(tubao[i+1],tubao[j])));}}printf("%lld\n",dis);
}int main(){scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%d%d",&node[i].x,&node[i].y);getTuBao();getMaxDis();
}