hdoj Surround the Trees 1392 (凸包)
2015-11-24 21:54
489 查看
Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9158 Accepted Submission(s): 3508
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
The minimal length of the rope. The precision should be 10^-2.
[align=left]Sample Input[/align]
9
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0
[align=left]Sample Output[/align]
243.06
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; struct zz { int x; int y; }q[1010],p[1010]; int cmp(zz a,zz b) { if(a.x==b.x) return a.y<b.y; return a.x<b.x; } int judge(zz a,zz b,zz c) { return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y); } int main() { int t,i,j; int n,r; while(scanf("%d",&n),n) { for(i=0;i<n;i++) scanf("%d%d",&q[i].x,&q[i].y); sort(q,q+n,cmp); int k1=0; for(i=0;i<n;i++) { while(k1>1&&judge(p[k1-2],p[k1-1],q[i])<=0) k1--; p[k1++]=q[i]; } int k2=k1; for(i=n-1;i>=0;i--) { while(k1>k2&&judge(p[k1-2],p[k1-1],q[i])<=0) k1--; p[k1++]=q[i]; } k1--; double sum=0; for(i=0,j=1;i<k1;i++,j++) { sum+=sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y)); } if(n>2) printf("%.2lf\n",sum); else printf("%.2lf\n",sum/2);//如果点的个数为二,只有一条线,不能组成闭合图,所以在计算时多算了一遍,在这块要除2. } return 0; }
相关文章推荐
- Easing圆环动画
- sudo tcp_wrappe
- Introduce to algorithm--------pseudo code to C/C++ code (chapter 24)
- 抛弃PHP,WordPress.com以JavaScript重写
- 第五届全国职工技能大赛-计算机程序设计员决赛总结
- Java邮件开发---基本概念
- 从大数据菜鸟走上大师的历程 Scala 第五讲 Array [此博文包含图片]
- codeforces 596E Wilbur and Strings(DFS)
- 32.CELL⾃适应⾼度
- android112 c代码打印日志,c反编译调用java
- Mockito实现原理探析 -- Mockito.when(...).thenReturn(...)的一个简化实现
- 使用requirejs加载多个插件
- 分配病房(C程序设计进阶第6周)
- Android任务和返回栈完全解析,细数那些你所不知道的细节
- NSnotification通知
- SpringMVC介绍之Validation
- eclipse连接oracle11g教程(win7系统)
- python基础_Scrapy爬虫基础学习一
- java中堆栈和队列的实现方式
- 响应式的前端框架bootstrap