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): 6728    Accepted Submission(s):
2556

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 凸包模板

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
 
Sample Output
243.06
 
Source
 
 
 
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std; struct node
{
int x,y;
};
node a[],stack1[]; double dis(node n1,node n2)//求距离
{
return (double)sqrt( (n1.x-n2.x)*(n1.x-n2.x)*1.0 + (n1.y-n2.y)*(n1.y-n2.y)*1.0 );
}
double cross(node a,node n1,node n2)// 为正时 "左转"
{
return (n1.x-a.x)*(n2.y-a.y) - (n1.y-a.y)*(n2.x-a.x);
}
bool cmp(node n1,node n2)// 叉乘越小的,越靠前
{
double k = cross(a[],n1,n2);
if( k>) return true;
else if( k== && dis(a[],n1)<dis(a[],n2))
return true;
else return false;
}
void Graham(int n)
{
int i,head;
double r=;
for(i=;i<n;i++)
if(a[i].x<a[].x ||(a[i].x==a[].x&&a[i].y<a[].y ) )
swap(a[],a[i]);
sort(a+,a+n,cmp); //排序
a[n]=a[]; //为了对最后一点的检验是否为满足凸包。cross(stack1[head-1],stack1[head],stack[i]);
stack1[]=a[];
stack1[]=a[];
stack1[]=a[];// 放入3个先
head=;
for(i=;i<=n;i++)
{
while( head>= && cross(stack1[head-],stack1[head],a[i])<= )head--;
// == 包含了重点和共线的情况。此题求周长,并没有关系。所以不加==,也是可以的。
stack1[++head]=a[i];
}
for(i=;i<head;i++) //不是<=. 因为 a[0]在 0 和 head 两个位置都出现了。
{
r=r+dis(stack1[i],stack1[i+]);
}
printf("%.2lf\n",r);
}
int main()
{
int i,n;
while(scanf("%d",&n)>)
{
if(n==)break;
for(i=;i<n;i++)
scanf("%d%d",&a[i].x,&a[i].y);//end input
if(n==)//特判
{
printf("0.00\n");
continue;
}
if(n==)//此题的特判
{
printf("%.2lf\n",dis(a[],a[]));
continue;
}
Graham(n);
}
return ;
}
上一篇:HDUJ 1392 Surround the Trees 凸包


下一篇:hdu 1392:Surround the Trees(计算几何,求凸包周长)