HDU-1392-Surround the Trees

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

Surround the Trees

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


 

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.

 樣例輸入輸出:


 

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

243.06

題目大意:給你一個樹林,問你最短需要多長的繩子能把整個樹林包起來

思路:看一下圖就感覺是凸包,我們可以使用Graham算法,掃描後計算長度(凸包試水)

注意兩點:一:當只有一個點或兩個點的時候要特判

二:算的時候不要忘記算最後一個點與第一個點的距離

代碼:


/*
題目大意:給你一個樹林,問你最短需要多長的繩子能把整個樹林包起來

思路:看一下圖就感覺是凸包,我們可以使用Graham算法,掃描計算長度(凸包試水)

*/
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<string>
#include<cstdio>
#include <vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll unsigned long long
#define inf 0x3f3f3f
#define esp 1e-8
#define bug {printf("mmp\n");}
#define mm(a,b) memset(a,b,sizeof(a))
#define T() int test,q=1;scanf("%d",&test); while(test--)
#define Test() {freopen("F:\\test.in","r",stdin);freopen("F:\\test1.out","w",stdout);}
const int maxn=2e4+10;
const double pi=acos(-1.0);
const int N=110;
const ll mod=1e9+7;
struct point
{
    int x,y;
} p[maxn];
int Stack[maxn],top;
int sign(double x)
{
    if(fabs(x)<esp)
        return 0;
    if(x<0)
        return -1;
    else
        return 1;
}
///距離
double dis(point a,point b)
{
    return sqrt((double)pow((a.x-b.x),2)+pow((a.y-b.y),2));
}
///叉積
int mulit(point a,point b,point c)
{
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
///極角排序:按照該點與x軸的夾角進行逆時針比較
bool cmp(point a,point b)
{
    int temp=mulit(p[0],a,b);
    if(temp>0)
        return true;
    if(temp==0&&dis(p[0],a)<dis(p[0],b))
        return true;
    return false;
}
///凸包
void tb(int n)
{
    point p0;
    int k;
    p0=p[0];
    for(int i=1; i<n; i++)
    {
        if(p0.y>p[i].y||(p0.y==p[i].y&&p0.x>p[i].x))
        {
            p0=p[i];
            k=i;
        }
    }
    p[k]=p[0];
    p[0]=p0;
    sort(p+1,p+n,cmp);
    if(n==1)
    {
        top=0;
        Stack[0]=0;
        return ;
    }
    if(n==2)
    {
        top=1;
        Stack[0]=0;
        Stack[1]=1;
        return ;
    }
    if(n>2)
    {
        for(int i=0; i<=1; i++)
            Stack[i]=i;
        top=1;
        for(int i=2; i<n; i++)
        {
            while(top>0&&mulit(p[Stack[top-1]],p[Stack[top]],p[i])<=0)
                top--;
            top++;
            Stack[top]=i;
        }
    }
}


int main()
{
    int n;
    while(scanf("%d",&n)&&n)
    {
        for(int i=0; i<n; i++)
            cin>>p[i].x>>p[i].y;
        if(n==1)
        {
            printf("0.00\n");
            continue;
        }
        if(n==2)
        {
            printf("%.2lf\n",dis(p[0],p[1]));
            continue;
        }
        tb(n);
        double ans=0;
        for(int i=0; i<top; i++)
            ans+=dis(p[Stack[i]],p[Stack[i+1]]);
        ans+=dis(p[Stack[0]],p[Stack[top]]);
        printf("%.2lf\n",ans);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章