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 ?
  • 相关阅读:
    Oracle锁表与解锁 对象锁与解锁
    Unity3D开发之NGUI点击事件穿透响应处理
    Unity 3D 关于给APK包加广告的流程
    Unity 3D 粒子系统的一点经验
    Unity3D模型的细致纹理问题解决办法
    Unity 3D学习之 Prime31 Game Center插件用法
    Unity3D如何制作透贴和使用透贴模型
    NGUI的部分控件无法更改layer?
    关于Unity3D中Resources动态加载NGUI图片的方法
    关于NGUI的动态加载后的刷新显示问题,解决办法!!
  • 原文地址:https://www.cnblogs.com/hlmark/p/3988440.html
Copyright © 2011-2022 走看看