hdu 1392(凸包周长)

Surround the Trees

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


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

思路:给出一定数量的树,可以看做点 来处理,求解把所有的树围起来,需要多长的绳子,可以先求解把所有树围起来的凸包,就知道了所有的点,然后在求解周长,注意当给出的点的数量为1时,需要的绳子的长度为0,当给出的点的数量为2时,需要的绳子的长度为两点之间的距离,当给出3个点的时候,就可以按照凸包计算。

代码:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define Max 105
struct p{
    int x;
    int y;
}point[Max];
int po[Max];
int Count[Max];
int cnt, n;
int cmp(p a, p b) {
    if(a.x != b.x)
        return a.x < b.x;
    else
        return a.y < b.y;
}
int xmult(p p0, p p1, p p2) {
    return (p1.x - p0.x)*(p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}
void convex() {
    int top;
    int i;
    sort(point, point+n, cmp);
    top = cnt = 0;
    po[top++] = 0;
    po[top++] = 1;
    for(i = 2; i < n; i++) {   //求解上凸包
        while(top > 1 && xmult(point[po[top-2]], point[po[top-1]], point[i]) < 0)
            top--;
        po[top++] = i;
    }
    for(i = 0; i < top; i++)
        Count[cnt++] = po[i];
    top = 0;
    po[top++] = n - 1;
    po[top++] = n - 2;
    for(i = n - 3; i >= 0; i--) {   //求解下凸包,当xmult <=0时表示共线的点取最远的,
                                    //当xmult<0时,表示共线的点取最近的。
        while(top > 1 && xmult(point[po[top-2]], point[po[top-1]], point[i]) < 0)
            top--;
        po[top++] = i;
    }
    for(i = 0; i < top; i++)
        Count[cnt++] = po[i];
}

int main() {
    while(scanf("%d", &n), n != 0) {
        int i;
        cnt = 0;
        double rex = 0.0;
        for(i = 0; i < n; i++) {
            scanf("%d%d", &point[i].x, &point[i].y);
        }
        if(n == 1) {        //当给出一个点的时候。
            printf("0.00\n");
            continue;
        }
        if(n == 2) {        //当给出两个点的时候。
            rex += sqrt(1.0*(point[0].x-point[1].x)*(point[0].x-point[1].x) +
                        (point[0].y - point[1].y)*(point[0].y - point[1].y));
            printf("%.2lf\n", rex);
            continue;
        }
        convex();
        for(i = 0; i < cnt-1; i++) {  //求解凸包的周长
            rex += sqrt(1.0*(point[Count[i]].x-point[Count[i+1]].x)*(point[Count[i]].x-point[Count[i+1]].x) +
                        (point[Count[i]].y - point[Count[i+1]].y)*(point[Count[i]].y - point[Count[i+1]].y));
        }
        printf("%.2lf\n", rex);
    }
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值