HDU 1392 HDU 1348 凸包

本文介绍如何计算凸包的周长,并针对n=1和n=2的特殊情况进行详细说明。通过实现cmp、point结构体、cmpless比较函数以及convex_hull函数,实现了凸包的高效计算。

求凸包的周长,  注意n=1 , 2时特殊情况


int  cmp(double x){
     if(fabs(x) < 1e-8) return 0 ;
     if(x > 0) return 1 ;
     return -1  ;
}

struct point{
       double  x ,  y  ;
       point(){}
       point(double _x , double _y):x(_x) , y(_y){}
       friend bool operator == (const point &a , const point &b){
              return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0 ;
       }
       friend double operator ^ (const point &a , const point &b){
              return a.x * b.y - a.y * b.x ;
       }
       friend double dist(const  point &a , const point &b){
              return  sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ) ;
       }
       point operator - (point o){
             return  point(x - o.x , y - o.y) ;
       }
};


bool  cmpless(const  point &a , const point &b){
      return   cmp(a.x - b.x) < 0
            || cmp(a.x - b.x == 0) && cmp(a.y - b.y) < 0 ;
}

vector<point> convex_hull(vector<point> a){
     vector<point> src(2 * a.size() + 5)  ;
     sort(a.begin() , a.end() , cmpless) ;
     a.erase(unique(a.begin() , a.end()) , a.end())  ;
     int m = 0 ;
     for(int i = 0 ; i < a.size() ; i++){
          while(m > 1 && cmp( (src[m-1] - src[m-2]) ^  (a[i] - src[m-2]) ) <= 0)
              m-- ;
          src[m++] = a[i] ;
     }
     int k = m ;
     for(int i = a.size() - 2 ; i >= 0 ; i--){
          while(m > k && cmp( (src[m-1] - src[m-2]) ^ (a[i] - src[m-2])) <= 0)
              m-- ;
          src[m++] = a[i] ;
     }
     src.resize(m) ;
     if(a.size() > 1) src.resize(m-1) ;
     return src ;
}

int   main(){
      int n  ,  i   ;
      double  ans ;
      while(cin>>n && n){
           vector<point> lis(n) ;
           for(i = 0 ; i < n ; i++)
                scanf("%lf%lf" , &lis[i].x , &lis[i].y) ;
           if(n == 1) ans = 0.0  ;
           else if(n == 2) ans =  dist(lis[0] , lis[1]) ;
           else{
                vector<point> a = convex_hull(lis) ;
                ans = 0.0 ;
                for(i = 0 ; i < a.size() ; i++)
                      ans += dist(a[i] , a[(i+1) % a.size()])  ;
           }
           printf("%.2lf\n" , ans) ;
      }
      return  0  ;
}


hdu 1348

int  cmp(double x){
     if(fabs(x) < 1e-8) return 0 ;
     if(x > 0) return 1 ;
     return -1  ;
}

struct point{
       double  x ,  y  ;
       point(){}
       point(double _x , double _y):x(_x) , y(_y){}
       friend bool operator == (const point &a , const point &b){
              return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0 ;
       }
       friend double operator ^ (const point &a , const point &b){
              return a.x * b.y - a.y * b.x ;
       }
       friend double dist(const  point &a , const point &b){
              return  sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ) ;
       }
       point operator - (point o){
             return  point(x - o.x , y - o.y) ;
       }
};


bool  cmpless(const  point &a , const point &b){
      return   cmp(a.x - b.x) < 0
            || cmp(a.x - b.x == 0) && cmp(a.y - b.y) < 0 ;
}

vector<point> convex_hull(vector<point> a){
     vector<point> src(2 * a.size() + 5)  ;
     sort(a.begin() , a.end() , cmpless) ;
     a.erase(unique(a.begin() , a.end()) , a.end())  ;
     int m = 0 ;
     for(int i = 0 ; i < a.size() ; i++){
          while(m > 1 && cmp( (src[m-1] - src[m-2]) ^  (a[i] - src[m-2]) ) <= 0)
              m-- ;
          src[m++] = a[i] ;
     }
     int k = m ;
     for(int i = a.size() - 2 ; i >= 0 ; i--){
          while(m > k && cmp( (src[m-1] - src[m-2]) ^ (a[i] - src[m-2])) <= 0)
              m-- ;
          src[m++] = a[i] ;
     }
     src.resize(m) ;
     if(a.size() > 1) src.resize(m-1) ;
     return src ;
}

int   main(){
      int n  ,  i  , t  , T = 1  ;
      double  ans  , r ;
      cin>>t  ;
      while(t--){
           cin>>n>>r ;
           vector<point> lis(n) ;
           for(i = 0 ; i < n ; i++)
                scanf("%lf%lf" , &lis[i].x , &lis[i].y) ;
           if(n == 1) ans = 0.0  ;
           else if(n == 2) ans =  dist(lis[0] , lis[1]) ;
           else{
                vector<point> a = convex_hull(lis) ;
                ans = 0.0 ;
                for(i = 0 ; i < a.size() ; i++)
                      ans += dist(a[i] , a[(i+1) % a.size()])  ;
           }
           if(T++ != 1) puts("") ;
           printf("%.0lf\n" , ans + 2.0 * acos(-1.0) * r) ;
      }
      return  0  ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值