Skip to content

Latest commit

 

History

History
41 lines (40 loc) · 922 Bytes

凸包.md

File metadata and controls

41 lines (40 loc) · 922 Bytes
const int maxn=11111;
double ans;
int n,k,l,u;
struct POINT
{
	double x,y;
	bool operator<(const POINT &b)
	{
		return (x<b.x||(x==b.x&&y<b.y));
	}
}point[maxn],ansl[maxn],ansu[maxn];//upper hull/lower hull
double cross(POINT& o,POINT& a,POINT& b){return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);}
double dist(POINT a,POINT b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}
void convex_hull(POINT p[],const int &k)
{
	sort(p,p+k+1);
	rep(i,0,k) 
	{
		while (l>=2&&cross(ansl[l-2],ansl[l-1],p[i])<=0) l--;
		while (u>=2&&cross(ansu[u-2],ansu[u-1],p[i])>=0) u--;
		ansl[l++]=p[i];ansu[u++]=p[i];
	}
}
double len_of_convex_hull()
{
	double res=0;
	rep(i,1,l-1) res+=dist(ansl[i],ansl[i-1]);
	rep(i,1,u-1) res+=dist(ansu[i],ansu[i-1]);
	return res;
}
int main()
{
	read(n);
	rep(i,0,n-1) scanf("%lf%lf",&point[i].x,&point[i].y);
	convex_hull(point,n-1);
	printf("%.2f\n",len_of_convex_hull());
	return 0;
}