Surround the Trees(凸包) (水平排序和极角排序)

There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.



There are no more than 100 trees.

Input

The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.

Zero at line for number of trees terminates the input for your program.

Output

The minimal length of the rope. The precision should be 10^-2.

Sample Input

9 
12 7 
24 9 
30 5 
41 9 
80 7 
50 87 
22 9 
45 1 
50 7 
0 

Sample Output

243.06

 

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;

typedef long long ll;
const int maxn=210;
struct Point{
    ll x,y;
    Point(ll _x=0,ll _y=0):x(_x),y(_y){}

    Point operator-(Point& obj){
        Point ans(x-obj.x,y-obj.y);
        return ans;
    }
    double dis(Point& obj){
        ll ans=(x-obj.x)*(x-obj.x)+(y-obj.y)*(y-obj.y);
        return sqrt(ans);
    }

}p[maxn];

ll Cross(Point a,Point b){//这里不能写引用,因为下面的临时变量会销毁
   return a.x*b.y-b.x*a.y;
}


/*
//基于水平序的Andrew算法
bool cmp(Point a,Point b){
    if(a.x==b.x)return a.y<b.y;
    return a.x<b.x;
}
double ConvexHull(Point* p,int n){
    sort(p,p+n,cmp);  //按照cmp规则进行排序
    int top=-1;
    Point stk[maxn];
    for(int i=0;i<n;i++){ //求一次下凸包
        Point a=stk[top]-stk[top-1],b=p[i]-stk[top-1];
        while(top>0&&Cross(stk[top]-stk[top-1],p[i]-stk[top-1])<0)top--;
        stk[++top]=p[i];
    }

    double ans=0;
    for(int i=1;i<=top;i++){
        ans+=stk[i].dis(stk[i-1]);
    }

    int m=top;
    for(int i=n-2;i>=0;i--){ //再求一次上凸包
        while(top>m&&Cross(stk[top]-stk[top-1],p[i]-stk[top-1])<0)top--;
        stk[++top]=p[i];
    }
    for(int i=m+1;i<=top;i++){
      ans+=stk[i].dis(stk[i-1]);
    }
    return ans;
}
*/

//**推荐写法
//进行极角排序
const double eps=1e-10;
bool cmp(Point& a,Point& b){
    long double A=atan2(a.y-p[0].y,a.x-p[0].x);
    long double B=atan2(b.y-p[0].y,b.x-p[0].x);
    if(fabs(A-B)<eps)return a.x<b.x;
    return A<B;
}
double ConvexHull(Point* p,int n){
    int pos=0;Point tmp=p[0];
    for(int i=0;i<n;i++){
        if(p[i].x<tmp.x||p[i].x==tmp.x&&p[i].y<tmp.y){
            pos=i,tmp=p[i];
        }
    }
    swap(p[pos],p[0]);//找到左下的那个点,放到第一个位置,其余的点,根据与第一个点的角度来排序
    sort(p+1,p+n,cmp);//进行极角排序

   // for(int i=0;i<n;i++) printf("i:%d (%lld,%lld)\n",i,p[i].x,p[i].y);
    Point stk[maxn];int top=-1;
    for(int i=0;i<n;i++){
        while(top>0&&Cross(stk[top]-stk[top-1],p[i]-stk[top-1])<=0)top--;//这里叉积到底取不取等于号,取得话,就是保留线中间的点
       // printf("i:%d top:%d (%lld,%lld)\n",i,top,stk[top].x,stk[top].y);
        stk[++top]=p[i];
       // printf("(%lld,%lld)\n\n",stk[top].x,stk[top].y);
    }
    //printf("top:%d\n",top);
    double ans=0;
    stk[++top]=p[0];//为了形式统一,在最后加一个点,形成回路
    for(int i=1;i<=top;i++){
        ans+=stk[i].dis(stk[i-1]);
    }
    //for(int i=0;i<=top;i++)printf("i:%d (%lld,%lld)\n",i,stk[i].x,stk[i].y);
    return ans;
}


int main(){
    int n;
    while(scanf("%d",&n)==1&&n){

        for(int i=0;i<n;i++){
            ll x,y;
            scanf("%lld %lld",&x,&y);
            p[i]=Point(x,y);
        }
        if(n==1){
            printf("0.00\n");
            continue;
        }
        if(n==2){
            printf("%.2f\n",p[0].dis(p[1]));
            continue;
        }
        printf("%.2f\n",ConvexHull(p,n));
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值