首页 > 代码库 > Surround the Trees
Surround the Trees
Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7161 Accepted Submission(s): 2736
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.
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.
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
n=1和n=2的时候要特判。 #include <iostream> #include <string.h> #include <algorithm> #include <math.h> using namespace std; #define M 1000 int top; struct node { double x,y; }p[M],stack[M]; double L(node a,node b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } double multi(node a,node b,node c) { return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y); } int cmp(node a,node b) { if(multi(a,b,p[0])>0) return 1; if(multi(a,b,p[0])==0&&L(a,p[0])<L(b,p[0])) return 1; return 0; } void GS(node p[],node stack[],int n) { int i,k=0; node temp; for(i=0;i<n;i++) { if(p[i].y<p[k].y||((p[i].y==p[k].y)&&(p[i].x<p[k].x))) k=i; } temp=p[0]; p[0]=p[k]; p[k]=temp; sort(p+1,p+n,cmp); top=2; stack[0]=p[0],stack[1]=p[1],stack[2]=p[2]; for(i=3;i<n;i++) { while(top>1&&multi(p[i],stack[top],stack[top-1])>=0) top--; stack[++top]=p[i]; } } int main() { double t; int n,i,m; while(scanf("%d",&n)!=EOF&&n) { t=0; memset(p,0,sizeof(p)); memset(stack,0,sizeof(stack)); for(i=0;i<n;i++) { scanf("%lf %lf",&p[i].x,&p[i].y); } if(n==1) { printf("0.00\n"); continue; } if(n==2) { printf("%.2lf\n",L(p[0],p[1])); continue; } GS(p,stack,n); stack[top+1]=stack[0]; for(i=0;i<=top;i++) { t+=L(stack[i],stack[i+1]); } if(n>2) printf("%.2lf\n",t); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。