凸包模板:Andrew算法


// 依赖的计算几何模板
inline int dcmp(double x){
  constexpr double eps=1e-6;
  if (x<-eps)return -1;
  if (x>eps)return 1;
  return 0;
}
struct Point {
  double x,y;
  Point()=default;
  Point(double x,double y):x(x),y(y){}
  Point operator+(const Point& p){
    return Point(x+p.x,y+p.y);
  }
  Point operator-(const Point& p){
    return Point(x-p.x,y-p.y);
  }
  // dot mul.
  Point operator*(double k){
    return Point(x*k,y*k);
  }
  // cross mul.
  double operator*(const Point& p){
    return x*p.y-y*p.x;
  }
  double dist(const Point& p){
    const Point dp=*this-p;
    return sqrt(dp.x*dp.x+dp.y*dp.y);
  }
};
// Andrew算法
struct AndrewConvexHull {
  // some define.
  static const int N = 1e5+5;
  int stk[N],tp;
  bool used[N];
  void cal(Point* p, int n) {
    // init
    tp=0;
    for(int i=1;i<=n;i++)used[i]=0;
    //
    sort(p+1,p+1+n,[](const Point& a,const Point& b) {
      if (a.x!=b.x)return a.x<b.x;
      return a.y<b.y;
    });
    // 点1不标记为used
    // 等到计算上凸壳的时候会被自动标记上
    stk[++tp]=1;
    for(int i=2;i<=n;i++){
      // dcmp<0会保留共线的点
      // 如果改成dcmp<=0则不会保留共线的点
      while(tp>1&&dcmp((p[stk[tp]]-p[stk[tp-1]])*(p[i]-p[stk[tp]]))<=0) {
        used[stk[tp]]=0;
        tp--;
      }
      used[i]=1;
      stk[++tp]=i;
    }
    int ltp=tp;
    for(int i=n-1;i>=1;i--){
      if(used[i])continue;
      while(tp>ltp&&dcmp((p[stk[tp]]-p[stk[tp-1]])*(p[i]-p[stk[tp]]))<=0) {
        used[stk[tp]]=0;
        tp--;
      }
      used[i]=1;
      stk[++tp]=i;
    }
    // stk[1]=stk[tp]=1,这里去掉一个重复的p[1]
    if(tp>1)tp--;
  }
}T;

模板题:

P2742 [USACO5.1] 圈奶牛Fencing the Cows /【模板】二维凸包
在这里插入图片描述

解法:
最小边长即凸包的边长。
计算出凸包之后,将凸包上相邻两点的距离算出并加入答案即可。
Code:
#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define int long long
#define PI pair<int, int>
const int maxm = 2e5 + 5;

// 依赖的计算几何模板
inline int dcmp(double x){
  constexpr double eps=1e-6;
  if (x<-eps)return -1;
  if (x>eps)return 1;
  return 0;
}
struct Point {
  double x,y;
  Point()=default;
  Point(double x,double y):x(x),y(y){}
  Point operator+(const Point& p){
    return Point(x+p.x,y+p.y);
  }
  Point operator-(const Point& p){
    return Point(x-p.x,y-p.y);
  }
  // dot mul.
  Point operator*(double k){
    return Point(x*k,y*k);
  }
  // cross mul.
  double operator*(const Point& p){
    return x*p.y-y*p.x;
  }
  double dist(const Point& p){
    const Point dp=*this-p;
    return sqrt(dp.x*dp.x+dp.y*dp.y);
  }
};
// Andrew算法
struct AndrewConvexHull {
  // some define.
  static const int N = 1e5+5;
  int stk[N],tp;
  bool used[N];
  void cal(Point* p, int n) {
    // init
    tp=0;
    for(int i=1;i<=n;i++)used[i]=0;
    //
    sort(p+1,p+1+n,[](const Point& a,const Point& b) {
      if (a.x!=b.x)return a.x<b.x;
      return a.y<b.y;
    });
    // 点1不标记为used
    // 等到计算上凸壳的时候会被自动标记上
    stk[++tp]=1;
    for(int i=2;i<=n;i++){
      // dcmp<0会保留共线的点
      // 如果改成dcmp<=0则不会保留共线的点
      while(tp>1&&dcmp((p[stk[tp]]-p[stk[tp-1]])*(p[i]-p[stk[tp]]))<=0) {
        used[stk[tp]]=0;
        tp--;
      }
      used[i]=1;
      stk[++tp]=i;
    }
    int ltp=tp;
    for(int i=n-1;i>=1;i--){
      if(used[i])continue;
      while(tp>ltp&&dcmp((p[stk[tp]]-p[stk[tp-1]])*(p[i]-p[stk[tp]]))<=0) {
        used[stk[tp]]=0;
        tp--;
      }
      used[i]=1;
      stk[++tp]=i;
    }
    // stk[1]=stk[tp]=1,这里去掉一个重复的p[1]
    if(tp>1)tp--;
  }
}T;
int n;
Point a[maxm];
void solve(){
  cin>>n;
  for(int i=1;i<=n;i++){
    cin>>a[i].x>>a[i].y;
  }
  T.cal(a,n);
  double ans=0;
  for(int i=1;i<=T.tp;i++){
    int now=T.stk[i];
    int pre=(i>1?T.stk[i-1]:T.stk[T.tp]);
    ans+=a[now].dist(a[pre]);
  }
  printf("%.2f\n",ans);
}
signed main() {
// #define MULTI_CASE
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("../in.txt", "r", stdin);
  freopen("../out.txt", "w", stdout);
#endif
#ifdef MULTI_CASE
  int T;
  cin >> T;
  while (T--)
#endif
    solve();
  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值