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 ?
  • 相关阅读:
    leetcode-mid-array-5. Longest Palindromic Substring
    leetcode-mid-array-334 Increasing Triplet Subsequence-NO
    leetcode-mid-array-3 Longest Substring Without Repeating Characters
    leetcode-mid-array-49 Group Anagrams
    leetcode-mid-array-73 set matrix zeros
    leetcode-mid-array-31 three sum-NO
    ANOVA-方差分析和单尾方差分析
    MTLD -词汇复杂度的指标
    shell脚本-2
    from __future__ import
  • 原文地址:https://www.cnblogs.com/hlmark/p/3988440.html
Copyright © 2011-2022 走看看