zoukankan      html  css  js  c++  java
  • hdu1392 凸包模板

    Surround the Trees

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


    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
     
    分析:Graham扫描法。
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    struct Node{
        double x,y;
    }p[200];
    int N,top;
    int res[200];
    bool cmp(Node A,Node B)//按极角排序,极角相同时比较与极点的距离 
    {
        if(A.y==B.y) return A.x<B.x; 
        return A.y<B.y;
    }
    
    bool mult(Node sp,Node ep,Node op)
    {//"右拐"返回1,元素出栈 
        return (sp.x-op.x)*(ep.y-op.y)>=(ep.x-op.x)*(sp.y-op.y);
    }
    
    void Graham()
    {
        sort(p,p+N,cmp);
        top=1;
        int len;
        if(N==0) return;res[0]=0;
        if(N==1) return;res[1]=1;
        if(N==2) return;res[2]=2;
        for(int i=2;i<N;i++)
        {
            while(top&&mult(p[i],p[res[top]],p[res[top-1]]))
                top--;
            res[++top]=i;
        }
        len=top;
        res[++top]=N-2;
        for(int i=N-3;i>=0;i--)
        {
            while(top!=len&&mult(p[i],p[res[top]],p[res[top-1]]))
                top--;
            res[++top]=i;
        }
    }
    
    double mul(double x1,double y1,double x2,double y2)
    {
        return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    }
    
    int main()
    {
        while(scanf("%d",&N)&&N)
        {
            for(int i=0;i<N;i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
            
            Graham();
            int s;
            for(s=0;s<top;s++)
                if(!p[res[s]].x&&!p[res[s]].y) break;
            double ans=0;
            for(int i=s+1;i<top;i++)
                ans+=mul(p[res[i-1]].x,p[res[i-1]].y,p[res[i]].x,p[res[i]].y);
            for(int i=1;i<=s;i++)
                ans+=mul(p[res[i-1]].x,p[res[i-1]].y,p[res[i]].x,p[res[i]].y);
            printf("%.2f
    ",ans);
        }
        return 0;
    }
    View Code
     
     
     
     
  • 相关阅读:
    Java Excel导入
    Git在Eclipse中忽略文件提交
    Git客户端安装(仅限windows用户)
    Java输出流文件下载
    Centos6.3源码安装Mysql-5.5.34
    Centos6.3安装Mongodb2.2.4
    Jacob操作Word各格式转换参数
    sublime text3使用小结
    获得select下拉框的值
    sublim text3 配置
  • 原文地址:https://www.cnblogs.com/ACRykl/p/9560143.html
Copyright © 2011-2022 走看看