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 ...
随机推荐
- Linux 常用命令笔记 (持续更新)
声明:本文是转载前辈的,地址:http://www.cnblogs.com/tovep/articles/2473147.html 在tomcat的bin目录下执行 ./shutdown.sh 为了查 ...
- 9.13 JS循环
循环:循环操作某一个功能(执行某段代码) 四要素: 循环初始值 循环条件 状态改变 循环体 for 穷举 迭代 i++;等价于i=i+1; ++I;等价于 ...
- MySQL的mysqldump工具的基本用法
导出要用到MySQL的mysqldump工具,基本用法是: shell> mysqldump [OPTIONS] database [tables] 如果你不给定任何表,整个数据库将 ...
- Wget下载终极用法和15个详细的例子
Wget下载终极用法和15个详细的例子 备注:wget 不支持https 下载,也没有相关https参数,当下载https的时候或以改用 axelWget是一种很好用的因特网下载工具,他具有的很多特 ...
- SMTP邮箱验证错误解决
开始报错,是因为权限设置问题,谷歌对第三方应用登录默认关闭,需要开通后python才能自动访问邮件 SMTPAuthenticationError: (502, b'5.5.1 Unrecognize ...
- HTML学习笔记——列表和table
1>有序列表.无序列表和自定义列表 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...
- GIT安装完需要做以下配置
安装完GIT后需要做以下配置: 一.找到git的安装目录,查找etc目录下的gitconfig配置文件,编辑此文件在最后一行添加如下内容: [gui] encoding = utf-8 [sv ...
- ssh远程连接ubuntu
1. 首先在服务器上安装ssh的服务器端. $ sudo aptitude install openssh-server 2. 启动ssh-server. $ sudo /etc/init.d/ss ...
- HBase中的压缩算法比较 GZIP、LZO、Zippy、Snappy [转]
网址: http://www.cnblogs.com/panfeng412/archive/2012/12/24/applications-scenario-summary-of-compressio ...
- 经纬度距离计算Java实现代码
public class test { private static double rad(double d) { return d * Math.PI / 180.0; } public stati ...