Surround the Trees

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
 

题意:

简单的凸包模板题目

题解:

这里介绍一种求凸包的算法:Graham。(相对于其它人的解释可能会有一些出入,但大体都属于这个算法的思想,同样可以解决凸包问题)

相对于包裹法的n*m时间,Graham算法在时间上有很大的提升,只要n*log(n)时间就够了。它的基本思想如下:

1、首先,把所有的点按照y最小优先,其次x小的优先排序

2、维护一个栈,用向量的叉积来判断新插入的点跟栈顶的点哪个在外围,如果栈顶的点在当前插入的点的左边,那么把栈顶的这个元素弹出,弹出之后不能继续插入下一个点,要继续判断当前插入点跟弹出之后的栈顶的点的位置关系,当当前插入的点在栈顶的那个点的左边时,则可以将要插入的点压到栈中,进入下一个点。

http://blog.csdn.net/bone_ace/article/details/46239187

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+, M = , mod = 1e9 + , inf = 0x3f3f3f3f;
typedef long long ll;
struct point{
double x,y;
point (double x = , double y = ):x(x),y(y) {}
friend point operator + (point a,point b) {
return point(a.x+b.x,a.y+b.y);
}
friend point operator - (point a,point b) {
return point(a.x-b.x,a.y-b.y);
}
}p[N],res[N];
double dis(point a,point b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double dot(point a,point b) {
return a.x*b.y-b.x*a.y;
}
int cmp(point a,point b) {
if(a.y==b.y) return a.x<b.x;
else return a.y<b.y;
}
int Graham(point* p,int n,point* res) {
sort(p+,p+n+,cmp);
res[] = p[];
res[] = p[];
int top = ,len;
for(int i=;i<=n;i++) {
while(top>= && dot(p[i] - res[top-],res[top] - res[top-])>=) top--;
res[++top] = p[i];
}
len = top;
for(int i=n;i>=;i--) {
while(top!=len&&dot(p[i]-res[top-],res[top]-res[top-])>=) top--;
res[++top] = p[i];
}
return top;
}
int main() {
int n;
while(scanf("%d",&n)&&n) {
for(int i=;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
if(n==) {
printf("0.00\n");continue;
}
if(n==) {
printf("%.2f\n",dis(p[],p[n]));
continue;
}
int m=Graham(p,n,res);
double tot=;
for(int i=;i<=m;i++) tot+=dis(res[i-],res[i]);
printf("%.2f\n",tot);
}
}

最新文章

  1. Handlebars模板库浅析
  2. Java随学随记
  3. Mybatis学习——传递Map型参数
  4. SilverLight命名空间详解-新手入门
  5. Java包的命名规则
  6. android 6.0获取 WRITE_SETTINGS 权限
  7. Android SharedPreferences登录记住密码
  8. 什么是FastCGI?
  9. 基于express框架的应用程序骨架生成器介绍
  10. Linux常用C函数---内存控制篇
  11. JavaScript中创建命名空间
  12. 面向对象程序设计-C++_课时24多态的实现
  13. php中empty和isset的区别
  14. spring学习笔记(一) Spring概述
  15. pthread_cond_wait虚假唤醒
  16. 上海高校程序设计联赛 D-CSL的字符串 栈模拟
  17. BZOJ2783: [JLOI2012]树(树上前缀和+set)
  18. python生成指定文件夹目录树
  19. C#异步的世界(重点:新异步)
  20. chromium ③

热门文章

  1. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩
  2. redis主从复制,读写分离
  3. nodejs免费空间
  4. WSL初体验
  5. 知网下载pdf文件的方法
  6. jQuery进度条设置
  7. BZOJ 4012 树链剖分+主席树
  8. POJ 2353 DP
  9. HD-ACM算法专攻系列(23)——Crixalis&#39;s Equipment
  10. Node.js获取本机IP