【模板】Graham凸包扫描法

参考题目:洛谷P2742


解析:

板子就是我这个博客上万年不填的坑啊。
J a r r i s Jarris Jarris步进法


代码:

#include<bits/stdc++.h>
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=10002;
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;}
	double dot(cs Point &b)cs{return x*b.x+y*b.y;}
	friend double cross(cs Point &a,cs Point &b){return a.x*b.y-a.y*b.x;} 
}O;

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 cmp1(cs Point &a,cs Point &b){
	return fabs(a.x-b.x)<eps?a.y<b.y:a.x<b.x;
}
inline
bool cmp2(cs Point &a,cs Point &b){
	return fabs((a-O)*(b-O))>eps?(a-O)*(b-O)>eps:(a-O).dot()<(b-O).dot();
}
struct Polygon{
	Point p[N];
	int n;
	
	Polygon convex_hull(){
		int m=1;
		sort(p+1,p+n+1,cmp1);
		O=p[1];
		sort(p+2,p+n+1,cmp2);
	
		for(int re i=2;i<=n;++i){
			while(m>=2&&cross(p[i]-p[m-1],p[m]-p[m-1])>-eps)--m;
			p[++m]=p[i];
		}
		n=m;
		O=Point();
		return *this;
	}
	
	double circu(){
		double res=0;
		for(int re i=2;i<=n;++i)res+=dist(p[i],p[i-1]);
		res+=dist(p[n],p[1]);
		return res;
	}
	
}poly;

signed main(){
	poly.n=getint();
	for(int re i=1;i<=poly.n;++i)poly.p[i].x=getdb(),poly.p[i].y=getdb();
	printf("%.2f\n",poly.convex_hull().circu());
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值