【模板】Graham凸包掃描法
阿新 • • 發佈:2019-02-12
解析:
程式碼:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define gc getchar
#define pc putchar
#define cs const
inline
int getint(){
re int num;
re char c;
re bool f=0;
while(!isdigit(c=gc()))f^=c=='-';num=c^48;
while(isdigit(c=gc()))num=(num<< 1)+(num<<3)+(c^48);
return f?-num:num;
}
inline
double getdb(){
re double x=0.0,y=1.0;
re char c;
re bool f=0;
while(!isdigit(c=gc()))f^=c=='-';
x=c^48;
while(isdigit(c=gc()))x=(x*10)+(c^48);
if(c!='.')return f?-x:x;
while(isdigit(c=gc()))x+=(y/=10)*(c^48);
return f?-x:x;
}
cs int N=10002;
cs double eps=1e-6;
struct Point{
double x,y;
Point(double _x=0,double _y=0):x(_x),y(_y){}
Point operator+(cs Point &b)cs{return Point(x+b.x,y+b.y);}
Point operator-(cs Point &b)cs{return Point(x-b.x,y-b.y);}
Point operator*(cs double &b)cs{return Point(x*b,y*b);}
double operator*(cs Point & b)cs{return x*b.y-y*b.x;}
double dot()cs{return x*x+y*y;}
double dot(cs Point &b)cs{return x*b.x+y*b.y;}
friend double cross(cs Point &a,cs Point &b){return a.x*b.y-a.y*b.x;}
}O;
inline
double dist(cs Point &a){
return sqrt(a.dot());
}
inline
double dist(cs Point &a,cs Point &b){
return sqrt((a-b).dot());
}
inline
bool cmp1(cs Point &a,cs Point &b){
return fabs(a.x-b.x)<eps?a.y<b.y:a.x<b.x;
}
inline
bool cmp2(cs Point &a,cs Point &b){
return fabs((a-O)*(b-O))>eps?(a-O)*(b-O)>eps:(a-O).dot()<(b-O).dot();
}
struct Polygon{
Point p[N];
int n;
Polygon convex_hull(){
int m=1;
sort(p+1,p+n+1,cmp1);
O=p[1];
sort(p+2,p+n+1,cmp2);
for(int re i=2;i<=n;++i){
while(m>=2&&cross(p[i]-p[m-1],p[m]-p[m-1])>-eps)--m;
p[++m]=p[i];
}
n=m;
O=Point();
return *this;
}
double circu(){
double res=0;
for(int re i=2;i<=n;++i)res+=dist(p[i],p[i-1]);
res+=dist(p[n],p[1]);
return res;
}
}poly;
signed main(){
poly.n=getint();
for(int re i=1;i<=poly.n;++i)poly.p[i].x=getdb(),poly.p[i].y=getdb();
printf("%.2f\n",poly.convex_hull().circu());
return 0;
}