Codeforces Beta Round #47 C凸包 (最终写法)

本文探讨了如何使用高效算法计算多个点构成的几何凸包,并对其内部点与凸包边界点之间的最短距离进行求解,旨在提供一种优化的空间几何问题解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意慢慢看。

typedef  long  long  LL ;

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

struct  point{
        double x , y ;
        point(){}
        point(double _x , double _y):x(_x) , y(_y){}
        point operator - (const point &o){
              return  point(x - o.x , y - o.y) ;
        }
        friend double operator ^ (const point &a , const point &b){
              return a.x * b.y - a.y * b.x ;
        }
        friend bool operator < (const point &a , const point &b){
              if(cmp(a.x - b.x) != 0)  return cmp(a.x - b.x) < 0 ;
              else   return  cmp(a.y - b.y) < 0 ;
        }
        friend bool operator == (const point &a , const point &b){
             return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0 ;
        }
        friend LL dist(const point &a , const point &b){
             return  (LL) max( fabs(a.x - b.x) , fabs(a.y - b.y)) ;
        }
};

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

int   main(){
      int i , n  ;
      double x ,  y  ;
      vector<point> lis ;
      cin>>n;
      for(i = 0 ; i < n ; i++){
          scanf("%lf%lf" , &x , &y) ;
          lis.push_back(point(x-1 , y)) ;
          lis.push_back(point(x+1 , y)) ;
          lis.push_back(point(x , y-1)) ;
          lis.push_back(point(x , y+1)) ;
      }

      vector<point> hull = convex_hull(lis)  ;
      LL  ans = 0  ;
      for(i = 0 ; i < hull.size() ; i++)
           ans += dist(hull[i] , hull[(i+1) % hull.size()]) ;
      cout<< ans << endl ;
      return  0 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值