【模板】旋转卡壳

参考题目:POJ2187


解析:

总觉得写得太水对不起别人,那等哪天想清楚了再写吧。。。
旋转卡壳核心代码其实很少的,就是PolygonPolygonPolygon类中的diadiadia,这只是求直径的一个模板。旋转卡壳还有很多其他玄妙的用途,会在以后更新的。


代码:

#include<iostream>
#include<cstdio>
#include<cctype>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
#define re register
#define gc getchar
#define pc putchar
#define cs const

inline
int getint(){
	re int num;
	re char c;
	re bool f=0;
	while(!isdigit(c=gc()))f^=c=='-';num=c^48;
	while(isdigit(c=gc()))num=(num<<1)+(num<<3)+(c^48);
	return f?-num:num;
}

inline
double getdb(){
	re double x=0.0,y=1.0;
	re char c;
	re bool f=0;
	while(!isdigit(c=gc()))f^=c=='-';x=c^48;
	while(isdigit(c=gc()))x=(x*10)+(c^48);
	if(c!='.')return f?-x:x;
	while(isdigit(c=gc()))x+=(y/=10)*(c^48);
	return f?-x:x;
}

cs int N=50008;
cs double eps=1e-6;

struct Point{
	double x,y;
	Point(double _x=0,double _y=0):x(_x),y(_y){}
	Point operator+(cs Point &b)cs{return Point(x+b.x,y+b.y);}
	Point operator-(cs Point &b)cs{return Point(x-b.x,y-b.y);}
	Point operator*(cs double &b)cs{return Point(x*b,y*b);}
	double operator*(cs Point &b)cs{return x*b.y-y*b.x;}
	double dot()cs{return x*x+y*y;}
	friend double dot(cs Point &a,cs Point &b){return a.x*b.x+a.y*b.y;}
	friend double cross(cs Point &a,cs Point &b){return a.x*b.y-a.y*b.x;} 
}O,q[N];

inline
double dist(cs Point &a){
	return sqrt(a.dot());
} 

inline
double dist(cs Point &a,cs Point &b){
	return sqrt((a-b).dot());
}

inline
bool cmp(cs Point &a,cs Point &b){
	return fabs(a.x-b.x)<eps?a.y<b.y:a.x<b.x;
}

struct Polygon{
	Point p[N];
	int n;
	
	Polygon convex_hull(Point *q,int m){
		sort(q+1,q+m+1,cmp);
		n=0;
		for(int re i=1;i<=m;++i){
			while(n>=2&&cross(q[i]-p[n-1],p[n]-p[n-1])>-eps)--n;
			p[++n]=q[i];
		}
		int k=n;
		for(int re i=m;i>=1;--i){
			while(n>k&&cross(q[i]-p[n-1],p[n]-p[n-1])>-eps)--n;
			p[++n]=q[i];
		}
		if(m>1)--n;
		return *this;
	}
	
	double dia(){
		double ans=0;
		p[n+1]=p[1];
		int now=2;
		for(int re i=1;i<=n;++i){
			while((p[i+1]-p[i])*(p[now]-p[i])<(p[i+1]-p[i])*(p[now+1]-p[i])){
				now++;
				if(now==n+1)now=1;
			}
			ans=max(ans,(p[now]-p[i]).dot());
		}
		return ans;
	}
}poly;

int m;
signed main(){
	m=getint();
	for(int re i=1;i<=m;++i)q[i].x=getint(),q[i].y=getint();
	poly.convex_hull(q,m);
	cout<<(int)poly.dia();
	return 0;
} 

转载于:https://www.cnblogs.com/zxyoi/p/10047270.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值