P2742-二维凸包/圈奶牛Fencing the Cows【凸包】

版权声明:原创,未经作者允许禁止转载 https://blog.csdn.net/Mr_wuyongcong/article/details/85627254

正题

题目链接:https://www.luogu.org/recordnew/lists?uid=SSL_WYC_zombieeeeee&pid=P2742&status=&sort=0


题目大意

求凸包总长度


解题思路

求凸包


c o d e code

#include<cstdio>
#include<algorithm>
#include<cmath>
#define N 10010
using namespace std;
struct point{
	double x,y;
}a[N];
int n,s[N];
double ans;
double m(point x,point y,point z)
{return (x.x-z.x)*(y.y-z.y)-(y.x-z.x)*(x.y-z.y);}
double dis(point x,point y)
{return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));}
bool cmp(point x,point y)
{
	double t=m(x,y,a[1]);
	if(t>0||t==0&&dis(x,a[1])<dis(y,a[1]))
	  return true;
	else return false;
}
void init()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
	  scanf("%lf%lf",&a[i].x,&a[i].y);
	  if(a[i].y<a[1].y||a[i].y==a[1].y&&a[i].x<a[1].x)
	    swap(a[i],a[1]);
	}
	sort(a+2,a+1+n,cmp);
}
void praham()
{
	s[1]=1;s[2]=2;s[3]=3;
	int top=3;
	for(int i=4;i<=n;i++)
	{
		while(m(a[i],a[s[top]],a[s[top-1]])>=0)
		  top--;
		s[++top]=i;
	}
	for(int i=1;i<top;i++)
	  ans+=dis(a[s[i]],a[s[i+1]]);
	printf("%.2lf",ans+dis(a[s[1]],a[s[top]]));
}
int main()
{
	init();
	praham();
}

猜你喜欢

转载自blog.csdn.net/Mr_wuyongcong/article/details/85627254