HDU 1392 Surround the Trees(凸包模板题)

 

Surround the Trees

 

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12658 Accepted Submission(s): 4904

 

 

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.



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

 

Source

Asia 1997, Shanghai (Mainland China) 

 

凸包的模板题,算出凸包后再算每条边的长度就可以了

凸包的详细解释:https://blog.csdn.net/u012328159/article/details/50808360  这个人讲的很详细

 

#include<bits/stdc++.h>

using namespace std;
const int INF = (1 << 30) - 1;
struct node
{
    int x,y;
}p[200000],S[200000];

int cmp(const node &a,const node &b) ///对极角进行排序
{
    double A = atan2((a.y-p[1].y),(a.x - p[1].x));
    double B = atan2((b.y-p[1].y),(b.x - p[1].x));
    if(A!=B)
        return A<B;
    else
        return a.x < b.x;
}

long long Cross(node a,node b,node c) ///计算叉积
{
    return 1LL*(b.x - a.x)*(c.y - a.y)-1LL*(c.x - a.x)*(b.y - a.y);
}

int Get(int n)
{
    p[0] = (node){INF,INF};int k ;
    for(int i = 1;i <= n;i ++) ///选出最小的点
    {
        if(p[i].y < p[0].y || (p[i].y == p[0].y && p[i].x < p[0].x))
        {
            k = i;
            p[0] = p[i];
        }
    }
    swap(p[k],p[1]);
    sort(&p[2],&p[n + 1],cmp); ///剩下的点进行排序
    S[0] = p[1];S[1] = p[2];int top = 1;

    for(int i = 3;i <= n;i ++)
    {
        while(top && Cross(S[top - 1],p[i],S[top]) >= 0)///如果栈内前两个点和第i个点的叉积大于0,说明最顶上的点不是最右边的点
        {
            top --;
        }

        S[++top] = p[i];
    }
    return top;
}

double dis(node a,node b)
{
    return sqrt(1LL *(b.x - a.x)*(b.x-a.x) + 1LL*(b.y - a.y)*(b.y - a.y) );
}



int main()
{
    int n;
    while(scanf("%d",&n) && n != 0)
    {
        for(int i = 1;i <= n;i ++)
        {
            scanf("%d %d",&p[i].x,&p[i].y);
        }

        if(n == 1)
        {
            printf("0\n");
            continue;
        }
        else if(n == 2)
        {
            printf("%.2f\n",dis(p[1],p[2]));
            continue;
        }

        n = Get(n);

        S[++n] = S[0];
        double sum = 0;
        for(int i = 1;i <= n;i ++)
        {
            sum += dis(S[i],S[i - 1]);
        }
        printf("%.2f\n",sum);

    }

    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值