hdoj1392Surround the Trees【凸包模板题】



Surround the Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8972    Accepted Submission(s): 3443


Problem Description
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<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
struct point{
    double x,y;
}A[110];
point result[110];
double dist(point a,point b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double cp(point p1,point p2,point p3){
    return (p3.x-p1.x)*(p2.y-p1.y)-(p3.y-p1.y)*(p2.x-p1.x);
}
bool cmp(point a,point b){
    double ans=cp(A[0],a,b);
    if(ans==0)
    	return dist(A[0],a)-dist(A[0],b)<=0;
    else
    	return ans>0;
}
int main()
{
    int n,i,j,k;
    while(scanf("%d",&n),n){
        int pos=0;
        for(i=0;i<n;++i){
            scanf("%lf%lf",&A[i].x,&A[i].y);
            if(A[pos].y>=A[i].y){
                if(A[pos].y==A[i].y){
                    if(A[pos].x>A[i].x)pos=i;
                }
                else pos=i;
            }
        }
        if(n==1){
            printf("0.00\n");
            continue;
        }
        if(n==2){
            printf("%.2lf\n",dist(A[0],A[1]));
            continue;
        }
        point temp;
        temp=A[pos];A[pos]=A[0];A[0]=temp;
        sort(A+1,A+n,cmp);
        int top=1;
        result[0]=A[0];
        result[1]=A[1];
        for(i=2;i<n;++i){
            while(cp(result[top-1],result[top],A[i])<0)top--;
            result[++top]=A[i];
        }
        double l=0;
        for(i=0;i<=top;++i){
            l+=dist(result[(i+1)%(top+1)],result[i]);
        }
        printf("%.2lf\n",l);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值