【bzoj1670】[Usaco2006 Oct]Building the Moat护城河的挖掘 凸包

本文详细介绍了裸的凸包算法的实现过程,包括结构定义、运算符重载、距离计算、排序和Graham扫描算法的实现,最终输出凸包周长。

裸的凸包啦,第一次写。


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#define maxn 5010 

using namespace std;

struct yts
{
	long long x,y;
}a[maxn],s[maxn];

long long operator*(yts x,yts y)
{
	return x.x*y.y-x.y*y.x;
}

yts operator-(yts x,yts y)
{
	yts ans;
	ans.x=y.x-x.x;ans.y=y.y-x.y;
	return ans;
}

double dis(yts x,yts y)
{
	return sqrt((double)(x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}

int n,m;
double ans;

bool cmp(yts x,yts y)
{
	int t=(x-a[1])*(y-a[1]);
	if (t==0) return dis(x,a[1])<dis(y,a[1]);
	return t<0;
}

void graham()
{
	int t=1;
	for (int i=2;i<=n;i++)
	  if (a[i].x<a[t].x || (a[i].x==a[t].x && a[i].y<a[t].y)) t=i;
	swap(a[t],a[1]);
	sort(a+2,a+n+1,cmp);
	int top=1;
	s[top]=a[1];
	for (int i=2;i<=n;i++)
	{
		while (top>1 && (s[top]-s[top-1])*(a[i]-s[top])>=0) top--;
		s[++top]=a[i];
	}
	s[top+1]=a[1];
	for (int i=1;i<=top;i++) ans+=dis(s[i],s[i+1]);
}

int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%lld%lld",&a[i].x,&a[i].y);
	graham();
	printf("%.2lf\n",ans);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值