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

HDU 1392 凸包子的更多相关文章

  1. HDU 1392 凸包模板题,求凸包周长

    1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...

  2. HDU 1392 Surround the Trees(凸包入门)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. HDU - 1392 Surround the Trees (凸包)

    Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...

  4. HDU 1392 Surround the Trees (凸包周长)

    题目链接:HDU 1392 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope ...

  5. HDU 1392 Surround the Trees(凸包*计算几何)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...

  6. hdu 1392 Surround the Trees

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意:给出一些点的坐标,求最小的凸多边形把所有点包围时此多边形的周长. 解法:凸包ConvexH ...

  7. HDU 1392 Surround the Trees(几何 凸包模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1392 题目大意: 二维平面给定n个点,用一条最短的绳子将所有的点都围在里面,求绳子的长度. 解题思路: 凸包的模 ...

  8. 题解报告:hdu 1392 Surround the Trees(凸包入门)

    Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...

  9. *HDU 1392 计算几何

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. 2014.08.04,读书,读书笔记-《Matlab概率与数理统计分析》-第1章 MATLAB的数据基础

    第1章 MATLAB数据基础 虽然一直间或使用MATLAB,但从来没有系统的学习过,现在开始也不晚.先对几个重点或者平时忽略的要点做下笔记. %后的所有文字为注释,多条命令可以放在一行,但要用逗号或分 ...

  2. js 预加载图片image()函数

    创建一个Image对象:var a=new Image();    定义Image对象的src: a.src=”xxx.gif”;    这样做就相当于给浏览器缓存了一张图片. 图像对象: 建立图像对 ...

  3. Mongo——C#操作

    自己练手写了一个MongoDb的泛型类,顺便把一些常用命令整理了一下,做个记录: /// <summary> /// Mongo操作类. /// </summary> /// ...

  4. ASP.net 中 OutputCache 指令各个参数的作用

    使用@ OutputCache指令 使用@ OutputCache指令,能够实现对页面输出缓存的一般性需要.@ OutputCache指令在ASP.NET页或者页中包含的用户控件的头部声明.这种方式非 ...

  5. 使用 Travis-CI 的五个理由

    I use the service of travis-ci now for a year. In that time the continuous integration has often poi ...

  6. Sqlite简单操作

    1.student类 public class Student { int id; String name; String sex; String address; int money; public ...

  7. 如何使用pgpool failover_stream.sh自己控制选择指定的master节点

    集群架构: h236:master h237:standby sync h238:standby sync h239:stadnby async h240:standby async h241:sta ...

  8. Html5必看:教你如何选择移动APP开发框架

    如何选择移动APP开发框架一直是困扰很多新手的难题,今天杭州APP开发小编就和大家一起分享一下HTML5 移动app开发过程中框架该如何选择?当然我们得先从下面几个方面来评估一个框架的优越性,然后再做 ...

  9. CDR中如何将对象在页面居中显示

    利用CorelDRAW在做设计排版时,如果想让对象在页面居中显示你会用什么方法?用鼠标拖?还是更准确的做法选择参照物对象,利用对齐与分布命令?或者还有更简单快速的方法,一起来看看吧! 最简单的方法(页 ...

  10. 关于npm警告fsevents和vue-cli项目中的一些问题,持续更新

    1.install一个npm包的时候,总是会报这个警告: 网上查资料知道,这个fsevents是mac下用的,windows忽略即可: 2.关于在main.js中引入less文件的问题, 就会报这个错 ...