萌新求助旋转卡壳
查看原帖
萌新求助旋转卡壳
226760
RedLycoris楼主2022/3/16 07:51

rt,本人刚学,尝试写了一发但一直WA6

求凸包用了Andrew算法,感觉都没啥问题啊

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mxn=2e5+5;
struct point{ll x,y;}p[mxn];
inline point getvec(point a,point b){return (point){b.x-a.x,b.y-a.y};}
inline ll dis(point a,point b){return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);}
inline ll xmul(point a,point b){return a.x*b.y-a.y*b.x;}
inline bool operator <(const point&a,const point&b){if(a.x==b.x)return a.y<b.y;return a.x<b.x;}
inline ll compare(point a,point b,point c){return xmul(getvec(c,a),getvec(c,b));}
int n,sta[mxn];
int main(){
	ios_base::sync_with_stdio(false);
	cin>>n;
	for(int i=1;i<=n;++i)cin>>p[i].x>>p[i].y;
	sort(p+1,p+n+1);
	ll ans=0;int cnt=0;
	sta[++cnt]=1,sta[++cnt]=2;
	for(int i=3;i<=n;++i){
		point u=getvec(p[sta[cnt-1]],p[sta[cnt]]);
		point v=getvec(p[sta[cnt]],p[i]);
		for(;xmul(u,v)<=0;){
			if(cnt==1)break;
			--cnt;
			u=getvec(p[sta[cnt-1]],p[sta[cnt]]);
			v=getvec(p[sta[cnt]],p[i]);
		}
		sta[++cnt]=i;
	}
	if(sta[cnt]==n)--cnt;
	int tmp=cnt;
	sta[++cnt]=n,sta[++cnt]=n-1;
	for(int i=n-2;i;--i){
		point u=getvec(p[sta[cnt-1]],p[sta[cnt]]);
		point v=getvec(p[sta[cnt]],p[i]);
		for(;xmul(u,v)<=0;){
			if(cnt==tmp+1)break;
			--cnt;
			u=getvec(p[sta[cnt-1]],p[sta[cnt]]);
			v=getvec(p[sta[cnt]],p[i]);
		}
		sta[++cnt]=i;
	}
	int t=2;
	for(int i=1;i<=cnt;++i){
		while(compare(p[sta[i]],p[sta[i+1]],p[sta[t]])<compare(p[sta[i]],p[sta[i+1]],p[sta[t%cnt+1]]))t=t%cnt+1;
		ans=max(ans,max(dis(p[sta[i]],p[sta[t]]),dis(p[sta[i+1]],p[sta[t]])));
	}
	cout<<fixed<<setprecision(9)<<ans<<'\n';
}
2022/3/16 07:51
加载中...