HDU 1392.Surround the Trees【凸包(求凸包周长)】【5月10】

Surround the Trees

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


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.

HDU 1392.Surround the Trees【凸包(求凸包周长)】【5月10】_第1张图片

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
 
不解释,凸包模板题(第一次做凸包):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct ss
{
    double x, y;
}po[105], s[105];
double xmulti(ss a, ss b, ss c)//向量叉积
{
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
double dis(ss a, ss b)//求距离
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp1(ss a, ss b)
{
    if(a.y == b.y) return a.x < b.x;
    else return a.y < b.y;
}
bool cmp(ss a, ss b)
{
    if(xmulti(po[0], a, b) > 0) return true;
    else if(xmulti(po[0], a, b) == 0)
    {
        if(dis(po[0], a) <= dis(po[0], b)) return true;
        else return false;
    }
    else return false;
}
int N, k;
double ans;
int main()
{
    while(scanf("%d", &N) != EOF && N)
    {
        k = 1;
        ans = 0;
        for(int i = 0;i < N; ++i)
        {
            scanf("%lf %lf", &po[i].x, &po[i].y);
        }
        if(N == 1)
        {
            printf("0.00\n");
            continue;
        }
        else if(N == 2)
        {
            printf("%.2f\n",  dis(po[0], po[1]));
            continue;
        }
        sort(po, po+N, cmp1);//找到最左下角的点
        sort(po+1, po+N, cmp);//按与x轴正方向的夹角排序
        memset(s, 0, sizeof(s));
        s[0] = po[0];
        s[1] = po[1];
        for(int i =  2;i < N; ++i)//做凸包
        {
            while(xmulti(s[k-1], s[k], po[i]) < 0 && k > 0) k--;
            s[++k] = po[i];
        }
        for(int i = 1;i <= k; ++i)//求距离和
        {
            ans += dis(s[i], s[i-1]);
        }
        ans += dis(s[0], s[k]);
        printf("%.2f\n", ans);
    }
    return 0;
}




你可能感兴趣的:(C++,ACM,HDU,凸包)