195人阅读 评论(0)

# 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;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
时刻记住--never give up
人生如棋，吾愿为卒，吾行虽慢，谁曾见吾后退半步！
个人资料
• 访问：195327次
• 积分：8863
• 等级：
• 排名：第2502名
• 原创：723篇
• 转载：3篇
• 译文：0篇
• 评论：27条