bzoj1670[Usaco2006 Oct]Building the Moat护城河的挖掘 凸包(Andrew)

7 篇文章 0 订阅
1 篇文章 0 订阅

裸的凸包,求出所有点以后直接相邻点求长度就可以了,注意不要忘记最后一个和第一个的。。

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const double eps=1e-7;
const int maxn=100005;
int n;
struct point{
    double x,y;
    point() {}
    point(double a,double b):x(a),y(b) {}
    bool operator<(const point&b)const{
        if (x<b.x) return 1;
        if (x>b.x) return 0;
        return y<b.y;
    }
    point operator-(const point&b) {return point(x-b.x,y-b.y);}
}a[maxn],stk[maxn];
typedef point vec;
int dcmp(double x){
    if (fabs(x)<=eps) return 0;
    return x>0?1:-1;
}
double getdst(point a,point b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double cross(vec a,vec b){
    return a.x*b.y-a.y*b.x;
}
int Andrew(){
    sort(a+1,a+1+n);
    int len=0;
    for (int i=1;i<=n;i++){
        while (len>1&&dcmp(cross(stk[len]-stk[len-1],a[i]-stk[len-1]))==-1) len--;
        stk[++len]=a[i];
    }
    int k=len;
    for (int i=n-1;i>=1;i--){
        while (len>k&&dcmp(cross(stk[len]-stk[len-1],a[i]-stk[len-1]))==-1) len--;
        stk[++len]=a[i];
    }
    return len;
}
int main(){
    scanf("%d",&n);
        for (int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y);
        int t=Andrew();
        double ans=0;
        for (int i=1;i<t;i++) ans+=getdst(stk[i],stk[i+1]);
        //for(int i=1;i<=t;i++)printf("%.1lf %.1lf\n",stk[i].x,stk[i].y);
        printf("%.2lf\n",n==2?ans/2:ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值