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. Codeforces Round #FF (Div. 2):B. DZY Loves Strings

    B. DZY Loves Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. Node.js:连接 MySQL

    ylbtech-Node.js:连接 MySQL 1.返回顶部 1. Node.js 连接 MySQL 本章节我们将为大家介绍如何使用 Node.js 来连接 MySQL,并对数据库进行操作. 如果你 ...

  3. php的异步并行扩展swoole

    Swoole是PHP的异步并行扩展,有点像Node.js,但swoole既支持同步又支持异步,比node更强大.Swoole扩展是基于epoll高性能事件轮询,并且是多线程的,性能非常好. Swool ...

  4. Spark任务调度

    不多说,直接上干货! Spark任务调度 DAGScheduler 构建Stage—碰到shuffle就split 记录哪个RDD 或者Stage 输出被物化 重新提交shuffle 输出丢失的sta ...

  5. 《鸟哥的Linux私房菜》笔记——03. 磁盘分区

    Everything is a file. 常见硬件对应于 Linux 下的文件(/dev目录下) 装置 装置在Linux内的档名 SCSI/SATA/U盘硬盘机 /dev/sd[a-p] U盘 /d ...

  6. hdu 3416 Marriage Match IV 【 最短路 最大流 】

    求边不可重复的最短路条数 先从起点到终点用一次dijkstra,再从终点到起点用一次dijkstra,来判断一条边是否在最短路上 如果在,就将这条边的两个端点连起来,容量为1 再跑一下dinic(), ...

  7. hook的本质就是在本原可执行文件中加东西

    hook的本质就是在本原可执行文件中加东西. 本质就是添加东西:

  8. LeetCode Golang 6. Z 字形变换

    6. Z 字形变换 将一个给定字符串根据给定的行数,以从上往下.从左到右进行 Z 字形排列. 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: L ...

  9. 使用json_decode无法解析json

    在接入合作方接口时,遇到一个json无法解析出来代码如下: <?php $res='{"resultcode":007,"resMsg":"!& ...

  10. Vue学习之路第三篇:插值表达式和v-text的区别

    上一篇说到插值表达式有一个问题: 页面频繁刷新或者网速加载很慢的时候,页面会先出现“{{ msg }}”,再一闪而过出现真实的数据. 对于这个问题Vue给予了解决办法,看具体事例. <div i ...