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 ...
随机推荐
- js013-事件
js013-事件 本章内容 理解事件流 使用时间处理程序 不同的事件类型 JS与HTML之间的交互是通过实践实现的.时间就还文档或浏览器窗口发生的一些特定的交互 瞬间.可以使用侦听器来预定事件,以便时 ...
- AspectJ基础学习之三HelloWorld(转载)
AspectJ基础学习之三HelloWorld(转载) 一.创建项目 我们将project命名为:aspectjDemo.然后我们新建2个package:com.aspectj.demo.aspect ...
- Java——文本组件:JTextComponent
import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing ...
- DllMaps
http://www.mono-project.com/docs/advanced/pinvoke/dllmap/ http://www.mono-project.com/docs/advanced/ ...
- yum安装指定(特定)版本(旧版本)软件包的方法
在命令行里输入: yum list SDL 注意这里类库的名字是区别大小写的. 参考 http://www.dabu.info/yum-install-specific-version-old-pac ...
- [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify uniq
angularjs 使用ng-repeat报错 <div ng-init="words = ['高校','高校','高校']" ng-repeat="word in ...
- 升级10.11.6后CocoaPods的坑,之前10.11.4已经安装好的,居然没了Failed to locate Homebrew!
升级10.11.6后CocoaPods的坑,之前10.11.4已经安装好的,居然没了,用命令 sudo gem install cocoapod 装不上,换 sudo gem install -n/u ...
- OSX10.11 删除系统自带的软件
之前一直用sudo rm - rf 系统的浏览器名字 正常删除safari 升级到10.11后,完全没作用了 需要关闭系统的什么安全模式 csrutil disable 再进入系统使用此命令可正常删除 ...
- oarcle数据库导入导出,创建表空间
oracle导入导出(前提,本机已经安装oracle和oracle客户端)导出命令: exp username/password@192.168.1.138/ORCL file=c:\test.dmp ...
- pch
#define kWeakSelf(weakSelf) __weak __typeof(self)weakSelf = self; #ifndef __OPTIMIZE__#define NSLog( ...