凸包

/*
ID:BLACK-WOOD
LANG:C++
TASK:fc
*/

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define esp 1e-8
#define MAXN 10010
using namespace std;
struct point
{
    double x,y;
    point(double _x = 0,double _y = 0) : x(_x),y(_y) {}
    void input() { scanf("%lf %lf",&x,&y); }
};
double sgn(double d)
{  return d > esp ? 1 : ( d < -esp ? -1 : 0 ) ;
}
point operator-(const point p1, const point p2)
{   return point(p1.x - p2.x, p1.y - p2.y);
}
double operator*(const point p1, const point p2)
{   return p1.x*p2.y - p1.y*p2.x;
}
double operator<(const point p1, const point p2)
{   if( sgn(p1.x - p2.x) != 0 ) return p1.x < p2.x ;
    else return sgn( p1.y - p2.y ) < 0;
}
int n,tn;
point a[MAXN],tu[MAXN];
void init()
{
    int i,j,k,r,w;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
       a[i].input(); 
}
void tubao()
{
    point hu[MAXN],hd[MAXN];
    int i,j,k,r,w,un,dn;
    sort(a+1,a+n+1);
    hu[1] = hd[1] = a[1];
    hu[2] = hd[2] = a[2];
    un = dn = 2;
    for(i=3;i<=n;i++)
    {  for(; un > 1 && sgn( (hu[un] - hu[un-1]) * (a[i] - hu[un]) ) <= 0 ; un--);
       for(; dn > 1 && sgn( (hd[dn] - hd[dn-1]) * (a[i] - hd[dn]) ) >= 0 ; dn--);
       hu[++un] = a[i];
       hd[++dn] = a[i];
    }
    tn = 0;
    for(i=1;i<=un-1;i++)  tu[++tn] = hu[i];
    for(i=dn;i>=2;i--)    tu[++tn] = hd[i];
}
void getans()
{
    int i,j,k;
    double ans=0;
    tu[0] = tu[tn];
    for(i=1;i<=tn;i++)
    {  ans += sqrt( (tu[i].x-tu[i-1].x)*(tu[i].x-tu[i-1].x) + (tu[i].y-tu[i-1].y)*(tu[i].y-tu[i-1].y) );
    }
    printf("%0.2f\n",ans);
}
int main()
{
    freopen("fc.in","r",stdin);
    freopen("fc.out","w",stdout);
    init();
    tubao();
    getans();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值