zoukankan      html  css  js  c++  java
  • HDU 1392 Surround the Trees

    Surround the Trees

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 193 Accepted Submission(s): 97
     
    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

    经典的几何凸包 。

    有一个注意的地方是两个点的时候不用乘以2 。  

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    
    typedef long long LL;
    
    const double eps = 1e-8;
    const int N = 1100;
    struct node
    {
        double x , y ;
        node(){};
        node operator - (const node &a) const{
            node res ;
            res.x = x - a.x ,res.y = y - a.y;
            return res;
        }
    
    }p[N],ch[N];
    
    int n ;
    inline bool cmp(node a , node b ){ if(a.x != b.x )return a.x < b.x ; else return  a.y < b.y; }
    inline double Cross( node a , node b ){ return a.x * b.y - a.y *b.x ; }
    inline double dis( node a , node b  ){ return sqrt( ( a.x- b.x ) *  ( a.x- b.x ) +  ( a.y- b.y ) * ( a.y- b.y ) ); }
    
    int ConvexHull()
    {
        sort( p , p + n ,cmp );
        int m = 0 ;
        for( int i = 0 ;i < n ; ++i ){
            while( m > 1 && Cross( ch[ m-1 ] - ch[m-2] , p[i] - ch[m-2] ) <= 0  ) m-- ;
            ch[m++] = p[i];
        }
        int k = m ;
        for( int i = n - 2 ; i >= 0 ; i -- ){
            while ( ( m > k ) &&  Cross( ch[m-1] - ch[m-2] , p[i] - ch[m-2] ) <= 0 ) m--;
            ch[m++] = p[i];
        }
        if( n > 1 ) m--;
        return m;
    }
    
    int main()
    {
        double sum ;
    
        #ifdef LOCAL
            freopen("in","r",stdin);
        #endif
    
        while(~scanf("%d",&n)&&n)
        {
            for( int i = 0; i < n ;++i ){
                scanf("%lf%lf", &p[i].x, &p[i].y);
             }
    
            if( n == 1 ){  puts("0.00"); continue; }
            if( n == 2 ) { printf("%.2lf
    ",dis(p[0],p[1])); continue ;}
            int m = ConvexHull();
            double ans = 0 ;
            for( int i = 0 ; i < m ; ++i ){
                ans += dis( ch[i] ,ch[ (i+1) %m ]  );
            }
            printf("%.2lf
    ",ans);
        }
        return 0;
    }
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    gitlab文件夹的权限不要随便给777
    记python版本管理--pyenv
    centos7上基于kubernetes的docker集群管理
    centos下修改docker连接docker_host默认方式为tls方式
    微信公众帐号开发之一(java)
    java抓取12306火车余票信息
    对Word2Vec的理解
    软件工程课程助教总结
    2017面向对象程序设计(Java)第十七周助教工作总结
    2017面向对象程序设计(Java)第十三周助教工作总结
  • 原文地址:https://www.cnblogs.com/hlmark/p/3988440.html
Copyright © 2011-2022 走看看