【BZOJ1069】【SCOI2007】—最大土地面积(凸包+旋转卡壳)

传送门

考虑枚举任意22个点,那么只需要枚举第二个点的时候旋转卡壳就可以O(n)O(n)得到最远点对了

#include<bits/stdc++.h>
using namespace std;
inline int read(){
	char ch=getchar();
	int res=0,f=1;
	while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
	while(isdigit(ch))res=res*10+(ch^48),ch=getchar();
	return res*f;
}
const int N=2005;
const double pi=acos(-1);
const double eps=1e-8;
struct point{
	double x,y;
	point(double a=0,double b=0){
		x=a,y=b;
	}
	friend inline point operator +(const point &a,const point &b){
		return point(a.x+b.x,a.y+b.y);
	}
	friend inline point operator -(const point &a,const point &b){
		return point(a.x-b.x,a.y-b.y);
	}
	friend inline double operator *(const point &a,const point &b){
		return (a.x*b.y-a.y*b.x);
	}
	friend inline point operator *(const point &a,const double &b){
		return point(a.x*b,a.y*b);
	}
	friend inline double operator /(const point &a,const point &b){
		return a.x*b.x+a.y*b.y;
	}
	inline double calc(){
		return sqrt(x*x+y*y);
	}
}p[N],q[N];
inline bool comp(const point &a,const point &b){
	double t=(a-p[1])*(b-p[1]);
	if(t==0)return (a-p[1]).calc()<(b-p[1]).calc();
	return t<0;
}
int n,m,top;
inline void graham()
{
	int k=1;
	for(int i=2;i<=n;i++)
		if(p[k].y>p[i].y||(p[k].y==p[i].y&&p[k].x>p[i].x))
			k=i;
	swap(p[1],p[k]);
	sort(p+2,p+n+1,comp);
	q[++top]=p[1];q[++top]=p[2];
	for(int i=3;i<=n;i++)
	{
		while(top>1&&(p[i]-q[top-1])*(q[top]-q[top-1])<=0)
			top--;
		q[++top]=p[i];
	}
}
double calc(){
	q[top+1]=p[1];
	double ans=0;
	int a,b;
	for(int x=1;x<=top;x++){
		a=x%top+1;b=(x+2)%top+1;
		for(int y=x+2;y<=top;y++){
			while(a%top+1!=y&&(q[y]-q[x])*(q[a+1]-q[x])>(q[y]-q[x])*(q[a]-q[x]))
				a=a%top+1;
			while(b%top+1!=x&&(q[b+1]-q[x])*(q[y]-q[x])>(q[b]-q[x])*(q[y]-q[x]))
				b=b%top+1;
			ans=max((q[y]-q[x])*(q[a]-q[x])+(q[b]-q[x])*(q[y]-q[x]),ans);
		}
	}
	return ans;
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%lf%lf",&p[i].x,&p[i].y);
	graham();
	printf("%.3lf",calc()/2);
	return 0;
}
posted @ 2019-02-21 22:03  Stargazer_cykoi  阅读(125)  评论(0编辑  收藏  举报