HDU 1392 Surround the Trees(凸包*计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392
这里介绍一种求凸包的算法:Graham。(相对于其它人的解释可能会有一些出入,但大体都属于这个算法的思想,同样可以解决凸包问题)
相对于包裹法的n*m时间,Graham算法在时间上有很大的提升,只要n*log(n)时间就够了。它的基本思想如下:
1、首先,把所有的点按照y最小优先,其次x小的优先排序
2、维护一个栈,用向量的叉积来判断新插入的点跟栈顶的点哪个在外围,如果栈顶的点在当前插入的点的左边,那么把栈顶的这个元素弹出,弹出之后不能继续插入下一个点,要继续判断当前插入点跟弹出之后的栈顶的点的位置关系,当当前插入的点在栈顶的那个点的左边时,则可以将要插入的点压到栈中,进入下一个点。
对于这题,最后要计算的是凸包的边长,所以最后别忘了加上最后一个点到第一个点的距离。还有只有一个点和两个点的情况时要进行特判。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = ;
struct point
{
double x,y;
point(double x = ,double y = ):x(x),y(y) {}
friend point operator + (point p1,point p2)
{
return point(p1.x+p2.x,p1.y+p2.y);
}
friend point operator - (point p1,point p2)
{
return point(p1.x-p2.x,p1.y-p2.y);
} }p[maxn],res[maxn];
double dis(point p1,point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
}
double dot(point p1,point p2)
{
return p1.x*p2.y - p2.x*p1.y;
}
bool cmp(point p1,point p2)
{
if(p1.y == p2.y) return p1.x < p2.x;
return p1.y < p2.y;
}
int graham(point* p,int n,point* res)
{
sort(p,p+n,cmp);
res[] = p[];
res[] = p[];
// res[2] = p[2];
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()
{
// freopen("in.txt","r",stdin);
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("%.2lf\n",dis(p[],p[]));
continue;
}
int m = graham(p,n,res);
double tot = ;
for(int i = ;i <= m;++i)
tot += dis(res[i-],res[i]);
printf("%.2lf\n",tot);
}
return ;
}
HDU 1392 Surround the Trees(凸包*计算几何)的更多相关文章
- hdu 1392:Surround the Trees(计算几何,求凸包周长)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU - 1392 Surround the Trees (凸包)
Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...
- 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 ...
- hdu 1392 Surround the Trees 凸包模板
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1392 Surround the Trees (凸包)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1392 Surround the Trees 凸包裸题
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1392 Surround the Trees(凸包入门)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDUJ 1392 Surround the Trees 凸包
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 计算几何(凸包模板):HDU 1392 Surround the Trees
There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So a ...
随机推荐
- HD1269迷宫城堡(有向图 && 划分连通块)
迷宫城堡 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- Yocto开发笔记之《快速入门,环境搭建 & 编译》(QQ交流群:519230208)
开了一个交流群,欢迎爱好者和开发者一起交流,转载请注明出处. QQ群:,为避免广告骚扰,申请时请注明 “开发者” 字样 ======================================== ...
- Java-IO之DeflaterOutputStream和InflaterOutputStream
此类为使用 "deflate" 压缩格式压缩数据实现输出流过滤器 example import java.io.File; import java.io.FileInputStre ...
- chmod 和 chown 的用法
一.chown 命令 用途:更改文件的所有者或组.命令由单词change owner组合而成. 使用示例: 1,更改文件的所有者: chown jim program.c 文件 program.c 的 ...
- JavaWeb学习笔记——XML解析
DOM解析操作 只在跟节点<addresslist>下面建立一个子节点<name> <?xml version="1.0" encoding=&quo ...
- 关于Spring常用的注解
参考文献:http://www.cnblogs.com/xdp-gacl/p/3495887.html 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationC ...
- Sort 整理
文章.图片参考:http://www.jianshu.com/p/1b4068ccd505?hmsr=toutiao.io&utm_medium=toutiao.io&utm_sour ...
- Unity3D PerRendererData
http://nordicedu.com/tkokblog/wordpress/?tag=perrendererdata MaterialPropertyBlock and SpriteRendere ...
- ecshop 获取某个商品的 所有订单信息 或者销量
把一下代码放到 lib_main.php 1.统计某个下单商品的人数 function get_goods_ordernum($goods_id){ $sql = "select count ...
- OC-copy
一,堆与栈 1,栈区,stack: 后进先出,由编译器自动分配并释放,一般存放函数的参数值.局部变量 2,堆区,heap:先进先出,由程序员分配和释放 3,全局区,静态区:程序结束后由系统释放, 4, ...