HDU1392

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392

凸包模板题,但是特别注意只有两个点的情况,MD两个点只能输出两点间的距离,可是题目上明明说的是surround围绕啊,MDZZ。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

const double eps = 1e-10;

struct Point
{
    double x, y;
    Point() {}
    Point(double x_, double y_): x(x_), y(y_) {}

    bool operator < (const Point& a) const
    {
        if (x == a.x)
            return y < a.y;
        return x < a.x;
    }

    bool operator == (const Point& a) const
    {
        if (x == a.x && y == a.y)
            return true;
        return false;
    }
};
typedef Point Vector;

Point P[110], convex[110];

Vector operator - (Point A, Point B)
{
    return Vector(A.x-B.x, A.y-B.y);
}

double Length(Point A, Point B)
{
    return sqrt((A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y));
}

double Cross(Vector A, Vector B) //A × B
{
    return A.x*B.y - A.y*B.x;
}

int ConvexHull(Point *p, int n, Point *ch)
{
    int m = 0;

    sort(p, p+n);
    n = unique(p, p+n) - p;

    for (int i=0; i<n; i++)
    {
        while (m > 1 && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
        ch[m++] = p[i];
    }

    int k = m;
    for (int i=n-2; i>=0; i--)
    {
        while (m > k && Cross(ch[m-1]-ch[m-2], p[i]-ch[m-2]) <= 0) m--;
        ch[m++] = p[i];
    }

    return m;
}

int main()
{
    int n;

    while (scanf("%d",&n))
    {
        if (!n)
            break;

        for (int i=0; i<n; i++)
            scanf("%lf%lf",&P[i].x, &P[i].y);

        if (n == 1)
        {
            printf("%.2lf\n",0);
            continue;
        }
        else if (n == 2)
        {
            printf("%.2lf\n",Length(P[0],P[1]));
            continue;
        }

        int cnt = ConvexHull(P, n, convex);
        double ans = 0;
        for (int i=1; i<cnt; i++)
            ans += Length(convex[i-1],convex[i]);

        printf("%.2lf\n",ans);
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值