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 ?
  • 相关阅读:
    第27章 结构型模式大PK
    第26章 创建型模式大PK
    Linux下C/C++代码调用PHP代码(转)
    php 5.2.17 升级到5.3.29
    PHP安全之register_globals (转)
    注意字符串的strlen与sizeof的差别
    嵌入式串口传送汉字乱码
    字符设备驱动模块与测试代码编写(转)
    字符设备之register_chrdev与register_chrdev_region(转)
    I2C子系统驱动框架及应用 (转)
  • 原文地址:https://www.cnblogs.com/hlmark/p/3988440.html
Copyright © 2011-2022 走看看