# Surround the Trees

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

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

ac代码：

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#define fab(a) (a)>0?(a):(-a)
#define LL long long
#define MAXN 55000
#define mem(x) memset(x,0,sizeof(x))
#define INF 0xfffffff
#define PI acos(-1.0)
using namespace std;
struct s
{
double x,y;
}list[MAXN];
int stack[MAXN],top;
double dis(s aa,s bb)
{
return sqrt((aa.x-bb.x)*(aa.x-bb.x)+(aa.y-bb.y)*(aa.y-bb.y));
}
int cross(s p0,s p1,s p2)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
bool cmp(s aa,s bb)
{
int num=cross(list[0],aa,bb);
if(num>0) return true;
else if(num==0&&dis(list[0],aa)<dis(list[0],bb)) return true;
else return false;
}
void graham(int n)
{
int i;
if(n==1) {top=0;stack[0]=0;}
if(n==2) {top=1;stack[0]=0;stack[1]=1;}
if(n>2)
{
for(i=0;i<2;i++)
stack[i]=i;
top=1;
for(i=2;i<n;i++)
{
while(top>0&&cross(list[stack[top-1]],list[stack[top]],list[i])<=0) top--;
top++;
stack[top]=i;
}
}
}
int main()
{
int n,i;
while(scanf("%d",&n)!=EOF,n)
{

s low;
scanf("%lf%lf",&list[0].x,&list[0].y);
low.x=list[0].x,low.y=list[0].y;
int k=0;
for(i=1;i<n;i++)
{
scanf("%lf%lf",&list[i].x,&list[i].y);
if(((low.y==list[i].y)&&(low.x>list[i].x))||low.y>list[i].y)
{
low.x=list[i].x;
low.y=list[i].y;
k=i;
}
}
if(n==2)
{
printf("%.2lf\n",dis(list[0],list[1]));
continue;
}
list[k]=list[0];
list[0]=low;
sort(list+1,list+n,cmp);
graham(n);
double ans=0;
for(i=0;i<top;i++)
{
ans+=dis(list[stack[i]],list[stack[i+1]]);
}
ans+=dis(list[stack[0]],list[stack[top]]);
//printf("%lf\n",ans);
printf("%.2lf\n",ans);
}
return 0;
}

#### 杭电OJ（HDOJ）1392题：Surround the Trees（凸包问题）

2014-12-01 18:30:27

#### HDU 1392 Surround the Trees（凸包模板）

2016-12-03 18:27:46

#### hdoj Surround the Trees 1392 （凸包）

2015-11-24 21:54:43

#### hdu1392 Surround the Trees (求凸包周长)

2015-12-03 16:55:26

#### (ssl1720)Surround the Trees(凸包问题，附带新手教程)

2018-01-13 14:02:52

#### Hdoj 1392 Surround the Trees

2018-01-07 16:47:46

#### hdoj 1392 Surround the Trees 【求凸包周长】

2015-12-23 11:49:41

#### HDUJ 1392 Surround the Trees 凸包

2014-07-18 15:07:34

#### hdu 1392 Surround the Trees【凸包】

2017-02-28 14:28:04

#### hdu 1392 Surround the Trees 凸包

2016-11-24 19:35:19

## 不良信息举报

HDOJ 1392 Surround the Trees (凸包)