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

凸包模板

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=5005;
int n,w,top;
struct dian
{
	double x,y;
	dian(double X=0,double Y=0)
	{
		x=X,y=Y;
	}
	dian operator + (const dian &a) const
	{
		return dian(x+a.x,y+a.y);
	}
	dian operator - (const dian &a) const
	{
		return dian(x-a.x,y-a.y);
	}
}a[N],s[N],d;
double cj(dian a,dian b)
{
	return a.x*b.y-a.y*b.x;
}
bool cmp(const dian &x,const dian &y)
{
	double ar=cj(x-d,y-d);
	return ar>0||(ar==0&&x.x<y.x);
}
double dis(dian a,dian b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int read()
{
	int r=0,f=1;
	char p=getchar();
	while(p>'9'||p<'0')
	{
		if(p=='-')
			f=-1;
		p=getchar();
	}
	while(p>='0'&&p<='9')
	{
		r=r*10+p-48;
		p=getchar();
	}
	return r*f;
}
int main()
{
	n=read();
	d.x=1e9,d.y=1e9;
	for(int i=1;i<=n;i++)
	{
		a[i].x=read(),a[i].y=read();
		if(a[i].x<d.x||(a[i].x==d.x&&a[i].y<d.y))
			w=i,d=a[i];
	}
	swap(a[w],a[n]);
	n--;
	sort(a+1,a+1+n,cmp);
	s[++top]=d;s[++top]=a[1];
	for(int i=2;i<=n;i++)
	{
		while(top>1&&cj(s[top]-a[i],s[top-1]-a[i])>=0)
			top--;
		s[++top]=a[i];
	}
	double ans=0;
	s[top+1]=s[1];
	for(int i=1;i<=top;i++)
		ans+=dis(s[i],s[i+1]);
	printf("%.2lf\n",ans);
	return 0;
}
posted @ 2018-05-04 09:22  lokiii  阅读(95)  评论(0编辑  收藏  举报