题目链接: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(凸包*计算几何)的更多相关文章

  1. hdu 1392:Surround the Trees(计算几何,求凸包周长)

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

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

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

  3. 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 ...

  4. hdu 1392 Surround the Trees 凸包模板

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

  5. hdu 1392 Surround the Trees (凸包)

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

  6. hdu 1392 Surround the Trees 凸包裸题

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

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

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

  8. HDUJ 1392 Surround the Trees 凸包

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

  9. 计算几何(凸包模板):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 ...

随机推荐

  1. 国家电力项目SSH搭建

    SSH项目框架搭建总结: 1.建立Web工程 * 导入需要的jar的包 db:连接数据库的驱动包 hibernate:使用hibernate的jar包 jstl:java的标准标签库 junit:测试 ...

  2. java编程思想-java集合总结-基本概念

    1.java 容器类类库的用途是"保存对象",并将其划分为两个不同的概念: 1)Collection.一个独立元素的序列,这些元素都服从一条或多条规则.List 必须按照插入的顺序 ...

  3. javascript之简单的选择排序法

    基本思想: 比对数组中元素,相等者输出元素在数组的下标,否则就输出没找到! 代码如下: function Orderseach(array,findVal){ var temp = false; // ...

  4. 转:Python K-means代码

    #coding: UTF-8 import pearson_distance from pearson_distance import pearson_distance from math impor ...

  5. 在spark-shell里用集群方式启动时加入用户需要的jar

    希望在spark-shell中测试集群方式的elasticsearch操作, # 1 首先下载相关的jar # 2 启动spark-shell时用--jars ./bin/spark-shell –m ...

  6. 兼容ie6及一下版本的自适应

    <meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1" /> <meta ...

  7. Java http方式提交短信到短信网关

    URL url = new URL("短信网关url"); 一般短信内容需要用URLEncoder.encode()编码一下 HttpURLConnection httpCon = ...

  8. 20145212 实验五《Java网络编程》

    20145212 实验五<Java网络编程> 一.实验内容 1.运行下载的TCP代码,结对进行,一人服务器,一人客户端: 2.利用加解密代码包,编译运行代码,一人加密,一人解密: 3.集成 ...

  9. header的安全配置指南

    0x00 背景 在统计了Alexa top 100万网站的header安全分析之后(2012年11月 - 2013年3月 - 2013年11月),我们发现其实如何正确的设置一个header并不是一件容 ...

  10. Python基本运算符

    Python基本运算符 什么是操作符? 简单的回答可以使用表达式4 + 5等于9,在这里4和5被称为操作数,+被称为操符. Python语言支持操作者有以下几种类型. 算术运算符 比较(即关系)运算符 ...