poj2187 Beauty Contest(旋轉卡殼)

模板題


注意:避免凸包上三點共線


代碼

#include<stdio.h>
#include<stdlib.h>
#define eps 1e-10
struct Point
{
	double x,y;
	Point()
	{
		x=y=0;
	}
};
typedef Point Vector;
Point P[50005],ch[50005];
double max(double a,double b)
{
	if(a>b) return a;
	return b;
}
Vector operator + (Vector a,Vector b)
{
	a.x+=b.x;
	a.y+=b.y;
	return a;
}
Vector operator - (Vector a,Vector b)
{
	a.x-=b.x;
	a.y-=b.y;
	return a;
}
bool operator < (Vector a,Vector b)
{
	return a.x<b.x || ( a.x==b.x && a.y<b.y );
}
bool operator > (Vector a,Vector b)
{
	return a.x>b.x || ( a.x==b.x && a.y>b.y );
}
int dcmp(double x)
{
	if(x<=eps&&x>=-eps) return 0;
	if(x>0) return 1;
	return -1;
}
double Cross(Vector a,Vector b)
{
	return a.x*b.y-b.x*a.y;
}
double L_2(Vector a)
{
	return a.x*a.x+a.y*a.y;
}
void kp(int low,int high)
{
	int i=low,j=high;
	Point mid=P[(i+j)/2],t;
	while(i<j)
	{
		while(P[i]<mid) i++;
		while(P[j]>mid) j--;
		if(i<=j)
		{
			t=P[i];
			P[i]=P[j];
			P[j]=t;
			i++;
			j--;
		}
	}
	if(j>low) kp(low,j);
	if(i<high) kp(i,high);
}
int main()
{
	double ans=0;
	int n,i,k,q=2,cnt=0;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		scanf("%lf%lf",&P[i].x,&P[i].y);
	kp(1,n);
	for(i=1;i<=n;i++)
	{
		while( cnt>1 && dcmp( Cross(ch[cnt]-ch[cnt-1],P[i]-ch[cnt-1]) )<=0 ) cnt--;
		ch[++cnt]=P[i];
	}
	k=cnt;
	for(i=n-1;i>=1;i--)
	{
		while( cnt>k && dcmp( Cross(ch[cnt]-ch[cnt-1],P[i]-ch[cnt-1]) )<=0 ) cnt--;
		ch[++cnt]=P[i];
	}
	for(i=1;i<cnt;i++)
	{
		while( Cross(ch[q+1]-ch[i+1],ch[i]-ch[i+1]) > Cross(ch[q]-ch[i+1],ch[i]-ch[i+1]) ) q=q%(cnt-1)+1;
		ans=max( ans , max( L_2(ch[q]-ch[i]) , L_2(ch[q]-ch[i+1]) ) );
	}
	printf("%.0lf",ans);
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章