hdu 1392(凸包)

时间:2023-03-09 19:27:21
hdu 1392(凸包)

传送门:Surround the Trees

题意:求凸包的周长。

分析:凸包模板题,先按极角排好序后,然后根据叉积正负确定凸包。

#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std; const double eps = 1e-;
const int N = ;
int sgn(double x)
{
if(fabs(x) < eps)return ;
if(x < )return -;
else return ;
}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y)
{
x = _x;y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
//叉积
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
//点积
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
};
double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
}
Point p[N];
int Stack[N],top;
bool cmp(Point p1,Point p2)
{
double tmp=(p1-p[])^(p2-p[]);
if(sgn(tmp)==)return sgn(dist(p[],p1)-dist(p[],p2))<=;
return sgn(tmp)>;
}
void Graham(int n)
{
sort(p+,p+n,cmp);
Stack[]=;
Stack[]=;
top=;
for(int i=;i<n;i++)
{
while(top>&&sgn((p[Stack[top-]]-p[Stack[top-]])^(p[i]-p[Stack[top-]]))<=)top--;
Stack[top++]=i;
}
}
int main()
{
int n;
while(scanf("%d",&n),n)
{
for(int i=;i<n;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
if(p[i].y<p[].y||p[i].y==p[].y&&p[i].x<p[].x)
swap(p[],p[i]);
}
if(n==)puts("0.00");
else if(n==)printf("%.2lf\n",dist(p[],p[]));
else
{
Graham(n);
double ans=;
for(int i=;i<top;i++)
ans+=dist(p[Stack[i]],p[Stack[(i+)%top]]);
printf("%.2lf\n",ans);
}
}
return ;
}