hdu 1392 (凸包模板)

Surround the Trees

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


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<iostream>
#include<string.h>
#include<cstdio>
#include<stdlib.h>
#include<cmath>
#include<algorithm>
using namespace std;
#define ll long long
#define rep(i,a,n) for(int i=(a);i<(n);++i)
#define per(i,a,n) for(int i=(n-1);i>=(a);--i)
const int Inf=1e9;
#define N 1020
struct node{
    int x,y;
}p[N],s[N];
double Cross(node a,node b,node c){
    return 1.0*(b.x-a.x)*(c.y-a.y)-1.0*(c.x-a.x)*(b.y-a.y);
}
double dis(node a,node b){
    return sqrt(1.0*(b.x-a.x)*(b.x-a.x)+1.0*(b.y-a.y)*(b.y-a.y));
}
bool cmp(node a,node b){           //按照极角排序,极角相同的,距离近的在前
    double t=Cross(p[0],a,b);
    if(t>0) return true;
    else if(t==0&&dis(p[0],a)<dis(p[0],b)) return true;
    return false;
}
int main(){
    ios::sync_with_stdio(0);
    int n;
    while(scanf("%d",&n),n){
        rep(i,0,n){
            int x0,y0;
            scanf("%d%d",&x0,&y0);
            p[i]=node{x0,y0};
        }
        if(n==1){
            printf("0.00\n");
            continue;
        }
        else if(n==2){
            printf("%.2f\n",dis(p[0],p[1]));
            continue;
        }
        int index=0;
        rep(i,0,n){
            if(p[i].y<p[index].y||(p[i].y==p[index].y&&p[i].x<p[index].x)){
                index=i;
            }
        }
        swap(p[index],p[0]);    //找到纵坐标最低的点,相同时,去横坐标最小的点
        sort(p+1,p+n,cmp);      //对剩余的n-1个点进行排序
        int top=0;
        s[top]=p[0];
        s[++top]=p[1];
        for(int i=2;i<n;++i){
            while(top&&Cross(s[top-1],p[i],s[top])>0){ //三点共线的情况Cross函数值为0
                --top;
            }
            s[++top]=p[i];
        }
        double ans=0;
        s[++top]=s[0];
        for(int i=1;i<=top;++i){
            ans+=dis(s[i],s[i-1]);
        }
        printf("%.2f\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u011721440/article/details/80309959
今日推荐