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
     
     
     
     
  • 相关阅读:
    《分析服务从入门到精通读书笔记》第五章、创建多维数据集之浏览数据(1)
    灵活运用SQL Server2008 SSIS变量
    SSIS几个通用属性
    [原创]Silverlight开发实践系列导航(提供源码)
    【转】Scott_ASP.NET MVC框架(第四部分) 处理表单编辑和提交场景
    C#数据结构(二)栈和队列
    C#数据结构(一)线性表
    django学习(一)
    谈谈电子商务网站的促销规则(定价策略)设计(三)
    GAE使用中的问题
  • 原文地址:https://www.cnblogs.com/ACRykl/p/9560143.html
Copyright © 2011-2022 走看看