HDU 1392 Surround the Trees (水平序Graham算法)
2016-02-06 22:38
225 查看
Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9419 Accepted Submission(s): 3613
[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
水平序 Graham 扫描算法
对顶点按x为第一关键字,y为第二关键字进行排序。
准备一个空栈,并将前两个点压入栈。
对于每一个顶点A,只要栈顶中还有至少两个顶点,记栈顶为T,栈中第二个为U。若(UT) ⃑x (TA) ⃑<=0,则将 T 弹出。重复此过程。
直到上一步不再弹出顶点,将A压入栈。扫描完一遍之后得到凸包的下凸壳。
将点集倒过来再进行一次,得到凸包的上凸壳,组合起来即可。
这题要注意n = 1,2的时候需要特判,1的时候好理解,至于2的时候需要对答案除以二,也就是只保留一个凸壳,不明白为什么??或许是用例有错?? 我是看了DISCUSS才发现的。。。感谢大神铺路【哭
下面是我又丑又长的代码。。。。
#include <iostream> #include <cstdio> #include <algorithm> #include <stack> #include <cmath> #define eps 1e-8 using namespace std; struct point { int x, y; }; bool cmp(point a, point b) { if(a.x != b.x) return a.x < b.x; else return a.y < b.y; } double getlen(point* trees,int n) { stack<point> S; while(!S.empty()) { S.pop(); } S.push(trees[0]); S.push(trees[1]); int i; point t1, t2; int flag = 0; for(i = 2; i < n; i++) { t1 = S.top(); S.pop(); t2 = S.top(); S.push(t1); if((t1.x - t2.x) * (trees[i].y - t1.y) - (trees[i].x - t1.x) * (t1.y - t2.y) <= 0) { S.pop(); flag = 1; } if(S.size() < 2 || flag == 0) { S.push(trees[i]); } else { i--; } flag = 0; } double len = 0; t1 = S.top(); S.pop(); while(!S.empty()) { t2 = S.top(); S.pop(); len += sqrt((t1.x - t2.x) * (t1.x - t2.x) * 1.0 + (t1.y - t2.y) * (t1.y - t2.y) * 1.0); t1 = t2; } return len; } int main() { int n; point trees[110]; point ttrees[110]; while(~scanf("%d", &n) && n) { int i; for(i = 0; i < n; i++) { scanf("%d %d", &trees[i].x, &trees[i].y); } if(n == 1) { printf("0.00\n"); continue; } sort(trees, trees + n, cmp); double len1 = getlen(trees, n); int j = n - 1; for(i = 0; i < n; i++, j--) { ttrees[j] = trees[i]; } double len2 = getlen(ttrees, n); if(n != 2) printf("%.2lf\n", len1 + len2); else printf("%.2lf\n", len1); } return 0; }
相关文章推荐
- 高性能架构
- 避免跨域的CDN部署方案
- 防刷票方案
- swift学习笔记之-枚举
- mysql获取更新日期为前一天的数据
- 抓取第三方网站数据
- axure笔记--内部框架交互链接
- 查看静态链接库内容
- 搜索(广度优先搜索)BFS 2
- 用rsync同步文件夹
- 谜题(Puzzle, ACM/ICPC World Finals 1993, UVa227)(难死了!!)
- BestCoder #71 KK's Number题解
- 在O(logn)时间内找到数组中离每个数最近而又比它大的数的下标
- nyoj--635--Oh, my goddess(dfs)
- javascript几个知识点
- 第十五天
- nyoj--635--Oh, my goddess(dfs)
- [BZOJ2527][Poi2011]Meteors
- win10 下runtime error 解决办法
- 在 java web 中调用存储过程