HDU 1392 Surround the Trees(求凸包周长)

10 篇文章 0 订阅

题目链接:
HDU 1392 Surround the Trees
题意:
求凸包周长。
分析:
需要注意只有一个点时答案是0,只有两个点时答案是两点间距离。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
using namespace std;
const int MAX_N=1100;

int n;

struct Point{
    double x,y;
    Point () {}
    Point (double x,double y) : x(x),y(y){
    }
    Point operator + (const Point& rhs) const {
        return Point(x+rhs.x,y+rhs.y);
    }
    Point operator - (const Point& rhs) const {
        return Point(x-rhs.x,y-rhs.y);
    }
    Point operator *(const double d) const {
        return Point(d*x,d*y);
    }
    double dis(const Point& rhs) const {
        return sqrt((x-rhs.x)*(x-rhs.x)+(y-rhs.y)*(y-rhs.y));
    }
    double cross(const Point& rhs) const {
        return (x*rhs.y-y*rhs.x);
    }
}point[MAX_N],vertex[MAX_N];

bool cmp_x(const Point& a,const Point& b)
{
    if(a.x==b.x) return a.y<b.y;
    else return a.x<b.x;
}

int Andrew()
{
    sort(point,point+n,cmp_x);
    int k=0;
    for(int i=0;i<n;i++){
        while(k>1&&(vertex[k-1]-vertex[k-2]).cross(point[i]-vertex[k-1])<=0) k--;
        vertex[k++]=point[i];
    }
    int m=k;
    for(int i=n-2;i>=0;i--){
        while(k>m&&(vertex[k-1]-vertex[k-2]).cross(point[i]-vertex[k-1])<=0) k--;
        vertex[k++]=point[i];
    }
    if(k>1) k--;
    return k;
}

void solve()
{
    int total=Andrew();
    //printf("total=%d\n",total);
    double perimeter=0;
    for(int i=0;i<total;i++){
        perimeter+=vertex[i].dis(vertex[(i+1)%total]);
    }
    printf("%.2f\n",perimeter);
}

int main()
{
    freopen("Gin.txt","r",stdin);
    while(~scanf("%d",&n)&&n){
        for(int i=0;i<n;i++){
            scanf("%lf%lf",&point[i].x,&point[i].y);
        }
        if(n==1){
            printf("0\n");
        }else if(n==2){
            printf("%.2f\n",point[0].dis(point[1]));
        }else {
            solve();
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值