[kuangbin]带你飞 计算几何之凸包问题 Wall POJ - 1113

Wall POJ - 1113
在这里插入图片描述
题意为求n个点的凸包周长和以l为半径的圆的周长
凸包Andrew算法模板

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
#define IOS ios::sync_with_stdio(0), cin.tie(0),cout.tie(0)
#define endl "\n"
#define x first
#define y second
#define pi 3.14159
typedef long long ll;
typedef pair<double ,double> PDD;
const int INF=0x3f3f3f3f;
const int N=2e5+5;
int n,l;
PDD q[N];//放点
int stk[N];//已经放进凸包的点的下标
bool used[N];//标记被使用过的点
double get_dist(PDD a,PDD b)//求距离
{
	double dx=a.x-b.x;
	double dy=a.y-b.y;
	return sqrt(dx*dx+dy*dy);
}
PDD operator-(PDD a,PDD b)//重写-方法
{
	PDD c;
	c.x=a.x-b.x;
	c.y=a.y-b.y;
	return c;
}
double cross(PDD a,PDD b)//求叉积
{
	return a.x*b.y-a.y*b.x;
}
double area(PDD a,PDD b,PDD c)
{
	return cross(b-a,c-a);
}
double andrew()
{
	sort(q,q+n);
	int top=0;
	for (int i=0;i<n;i++)
	{
		while(top>1 && area(q[stk[top-1]],q[stk[top]],q[i])<=0)
		{
			if (area(q[stk[top-1]],q[stk[top]],q[i])<0) 
				used[stk[top--]]=false;
			else top--;
		}
		stk[++top]=i;
		used[i]=true;
	}
	used[0]=false;
	for (int i=n-1;i>=0;i--)
	{
		if (used[i]) continue;
		while(top>1 && area(q[stk[top-1]],q[stk[top]],q[i])<=0) top--;
		stk[++top]=i;
	}
	double res=0;
	for (int i=2;i<=top;i++) res+=get_dist(q[stk[i-1]],q[stk[i]]);
	return res;
}
int main()
{
	IOS;
	cin>>n>>l;
	for (int i=0;i<n;i++) cin>>q[i].x>>q[i].y;
	double res=andrew();
	res+=2*l*pi+0.5;
	printf("%d\n",(int)res);
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值