欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Surround the Trees(HDU-1392)

程序员文章站 2022-04-01 15:50:28
...

关于凸包的一些讲解可以看这里:

https://blog.csdn.net/u013377068/article/details/80095620

https://www.cnblogs.com/blowhail/p/11209173.html

https://blog.csdn.net/ZCY19990813/article/details/98034495

Problem Description

There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So at first he must know the minimal required length of the rope. However, he does not know how to calculate it. Can you help him?
The diameter and length of the trees are omitted, which means a tree can be seen as a point. The thickness of the rope is also omitted which means a rope can be seen as a line.

 

Surround the Trees(HDU-1392)



There are no more than 100 trees.

Input

The input contains one or more data sets. At first line of each input data set is number of trees in this data set, it is followed by series of coordinates of the trees. Each coordinate is a positive integer pair, and each integer is less than 32767. Each pair is separated by blank.

Zero at line for number of trees terminates the input for your program.

Output

The minimal length of the rope. The precision should be 10^-2.

Sample Input

9 12 7 24 9 30 5 41 9 80 7 50 87 22 9 45 1 50 7 0

Sample Output

243.06

 

题意:让你求一个凸包的周长。

思路:这道题的话,读完题之后我们可以发现这是一个凸包的模板题。

AC代码:

#include <bits/stdc++.h>
typedef long long ll;
const int maxx = 100010;
const int inf = 0x3f3f3f3f;
using namespace std;
struct node
{
    double a, b;
} edge[maxx];            //数组edge用来记录所有的点
node res[maxx];          //数组res用来记录凸包的点
bool cmp(node x, node y) //当x坐标值不同的时候,将x坐标值从小到大排序
{                        //当x坐标值相同的时候,以y值小的在前面
    if (x.a != y.a)
        return x.a < y.a;
    return x.b < y.b;
}
double cj(node i, node j, node c) //计算叉积
{
    return (i.a - c.a) * (j.b - c.b) - (i.b - c.b) * (j.a - c.a);
}
double dis(node x, node y) //计算两点之间的距离
{
    return sqrt((x.a - y.a) * (x.a - y.a) + (x.b - y.b) * (x.b - y.b));
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    while (cin >> n, n)
    {
        for (int i = 0; i < n; i++)
            cin >> edge[i].a >> edge[i].b;

        if (n == 1)
        {
            cout << 0.00 << endl;
            continue;
        }
        if (n == 2)
        {
            cout << fixed << setprecision(2) << dis(edge[0], edge[1]) << endl;
            continue;
        }
        sort(edge, edge + n, cmp);
        res[0] = edge[0];
        res[1] = edge[1];
        int tot = 2;
        for (int i = 2; i < n; i++) //下凸包
        {
            while (tot >= 2 && cj(edge[i], res[tot - 1], res[tot - 2]) > 0)
                tot--;
            res[tot] = edge[i];
            tot++;
        }
        int pop = tot;
        for (int i = n - 2; i >= 0; i--) //上凸包
        {
            while (tot >= pop && cj(res[tot - 1], edge[i], res[tot - 2]) < 0)
                tot--;
            res[tot] = edge[i];
            tot++;
        }
        double sum = 0;
        tot--;
        for (int i = 0; i < tot; i++)
            sum += dis(res[i], res[i + 1]);
        cout << fixed << setprecision(2) << sum << endl;
    }
    return 0;
}

 

相关标签: 凸包 凸包