POJ 2187 Beauty Contest 旋转卡壳

12 篇文章 0 订阅
3 篇文章 0 订阅

题目大意:求平面上点集中的最远点对间距离的平方

第一次写旋转卡壳- - 能1A真是开心- -

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 50500
#define EPS 1e-7
#define INF 1e8
using namespace std;
struct Point{
	int x,y;
	friend istream& operator >> (istream &is,Point &p)
	{
		scanf("%d%d",&p.x,&p.y);
		return is;
	}
	bool operator < (const Point &p) const
	{
		if(x==p.x)
			return y<p.y;
		return x<p.x;
	}
	friend double Get_Slope(const Point &p1,const Point &p2)
	{
		if(p1.x==p2.x)
			return p1.y<p2.y?INF:-INF;
		return (double)(p1.y-p2.y)/(p1.x-p2.x);
	}
	friend int Distance(const Point &p1,const Point &p2)
	{
		return (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y);
	}
}points[M];
int n,ans;
Point *stack1[M],*stack2[M];
int top1,top2;
//stack1 上凸包
//stack2 下凸包
void Get_Convex_Hull()
{
	int i;
	{
		Point **stack=stack1;
		int &top=top1;
		for(i=1;i<=n;i++)
		{
			while( top>=2 && Get_Slope(*stack[top-1],*stack[top])<Get_Slope(*stack[top],points[i])+EPS )
				stack[top--]=0x0;
			stack[++top]=&points[i];
		}
	}
	{
		Point **stack=stack2;
		int &top=top2;
		for(i=1;i<=n;i++)
		{
			while( top>=2 && Get_Slope(*stack[top-1],*stack[top])>Get_Slope(*stack[top],points[i])-EPS )
				stack[top--]=0x0;
			stack[++top]=&points[i];
		}
	}
	
}
void Rotating_Calipers()
{
	int i,j;
	for(j=top2,i=1;i<=top1;i++)
	{
		double slope=(i==top1?-INF:Get_Slope(*stack1[i],*stack1[i+1]) );
		ans=max(ans,Distance(*stack1[i],*stack2[j]) );
		while( j>1 && Get_Slope(*stack2[j-1],*stack2[j])>slope )
			j--,ans=max(ans,Distance(*stack1[i],*stack2[j]) );
	}
}
int main()
{
	int i;
	cin>>n;
	for(i=1;i<=n;i++)
		cin>>points[i];
	sort(points+1,points+n+1);
	Get_Convex_Hull();
	Rotating_Calipers();
	cout<<ans<<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值