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
解题思路:入门凸包!有一篇博文讲得非常好非常易懂,链接:凸包详解。再贴一张如何用叉积来判断栈顶的点是否为凸包上合法顶点的依据图。时间复杂度主要花在排序上为O(nlogn)。
以下是Graham扫描算法的演示--->逐步构建凸包的过程:
AC代码(62ms):详细注释看代码。选取基点的标准是找到y值最小的点,如果有多个,选择最左边即横坐标最小的那个。
 #include<iostream>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=;
const double PI=acos(-1.0);
struct node{int x,y;};
node vex[maxn];//存入所有坐标点
node stackk[maxn];//凸包中所有的点
bool cmp1(node a,node b){//按点的坐标排序
if(a.y==b.y)return a.x<b.x;//如果纵坐标相同,则按横坐标升序排
else return a.y<b.y;//否则按纵坐标升序排
}
bool cmp2(node a,node b){//以基点为坐标原点,极角按升序排,这里可用atan2函数或者叉积来进行极角排序,但是用atan2函数来排序效率高时间快,不过精度比叉积低
double A=atan2(a.y-stackk[].y,a.x-stackk[].x);//返回的是原点至点(x,y)的方位角,即与x轴的夹角
double B=atan2(b.y-stackk[].y,b.x-stackk[].x);
if(A!=B)return A<B;//逆时针方向为正值,极角小的排在前面
else return a.x<b.x;//如果极角相同,则横坐标在前面的靠前排列
}
int cross(node p0,node p1,node p2){//计算两个向量a、b(a=(x1,y1),b=(x2,y2))的叉积公式:a×b=x1y2-x2y1 ===> p0p1=(x1-x0,y1-y0),p0p2=(x2-x0,y2-y0)
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dis(node a,node b){//计算两点之间的距离
return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
}
int main(){
int t;
while(~scanf("%d",&t)&&t){
for(int i=;i<t;++i)//输入t个点
scanf("%d%d",&vex[i].x,&vex[i].y);
if(t==)printf("%.2f\n",0.00);//如果只有一个点,则周长为0.00
else if(t==)printf("%.2f\n",dis(vex[],vex[]));//如果只有两个点,则周长为两个点的距离
else{
memset(stackk,,sizeof(stackk));//清0
sort(vex,vex+t,cmp1);//先按坐标点的位置进行排序
stackk[]=vex[];//取出基点
sort(vex+,vex+t,cmp2);//将剩下的坐标点按极角进行排序,以基点为坐标原点
stackk[]=vex[];//将凸包中的第二个点存入凸集中
int top=;//当前凸包中拥有点的个数为top+1
for(int i=;i<t;++i){//不断地找外围的坐标点
while(top>&&cross(stackk[top-],stackk[top],vex[i])<=)top--;//如果叉积为负数或0(0表示两向量共线),则弹出栈顶元素
//虽然第2个凸点显然是最外围的一点,但加上top>0保证了栈中至少有2个凸点
stackk[++top]=vex[i];
}
double s=;
for(int i=;i<=top;++i)//计算凸包的周长
s+=dis(stackk[i-],stackk[i]);
s+=dis(stackk[top],vex[]);//最后一个点和第一个点之间的距离
printf("%.2f\n",s);
}
}
return ;
}

AC代码二(31ms):Andrew算法,一次坐标排序,两次构造成一个完整的凸包,时间复杂度为O(nlogn),但实际上比Graham扫描算法快很多,具体讲解-->凸包解法总结

 #include<iostream>
#include<string.h>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=;
const double PI=acos(-1.0);
struct node{int x,y;}vex[maxn],stackk[maxn];
bool cmp(node a,node b){//按点的坐标排序
return (a.y<b.y)||(a.y==b.y&&a.x<b.x);
}
int cross(node p0,node p1,node p2){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
double dis(node a,node b){
return sqrt((a.x-b.x)*(a.x-b.x)*1.0+(a.y-b.y)*(a.y-b.y));
}
int main(){
int t;
while(~scanf("%d",&t)&&t){
for(int i=;i<t;++i)//输入t个点
scanf("%d%d",&vex[i].x,&vex[i].y);
if(t==)printf("%.2f\n",0.00);
else if(t==)printf("%.2f\n",dis(vex[],vex[]));
else{
memset(stackk,,sizeof(stackk));//清0
sort(vex,vex+t,cmp);//一次坐标排序,两次构造成一个完整的凸包
int top=-;
for(int i=;i<t;++i){//构造凸包下侧
while(top>&&cross(stackk[top-],stackk[top],vex[i])<=)top--;
stackk[++top]=vex[i];
}
for(int i=t-,k=top;i>=;--i){//构造凸包上侧,默认此时凸包中只有一个顶点n-1,因此top要大于k,起点再被包含一次且一定被包含
while(top>k&&cross(stackk[top-],stackk[top],vex[i])<=)top--;
stackk[++top]=vex[i];
}
double s=;
for(int i=;i<=top;++i)//计算凸包周长
s+=dis(stackk[i-],stackk[i]);
printf("%.2f\n",s);
}
}
return ;
}

题解报告:hdu 1392 Surround the Trees(凸包入门)的更多相关文章

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

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

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

  3. hdu 1392 Surround the Trees 凸包模板

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

  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(凸包*计算几何)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...

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

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

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

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

  9. HDUJ 1392 Surround the Trees 凸包

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

  10. HDU-1392 Surround the Trees,凸包入门!

    Surround the Trees 此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点. 题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内. 思路:简单凸包入门题,凸包 ...

随机推荐

  1. 十天学习PHP之第二天

    学习目的:学会构建数据库  在ASP中,假设是ACCESS数据库你能够直接打开ACCESS来编辑MDB文件,假设是SQL SERVER你能够打开企业管理器来编辑SQL SERVER数据库.可是在PHP ...

  2. aapt2 错误

    android.enableAapt2=false Error:Execution failed for task ':app:preDebugAndroidTestBuild'. > Conf ...

  3. vue中引入字体文件

    在用vue来写一官网的时候,想引入外部字体文件,毕竟总感觉他自己的字体有点难看,在这里记录下 1.先下载字体文件所需的.ttf文件 我这里想引入的是华文行楷字体 百度后下载了一个3M多的ttf文件 2 ...

  4. Linux 内核源码中likely()和unlikely()【转】

    本文转载自:http://blog.csdn.net/tigerjibo/article/details/8279183 ikely()与unlikely()在2.6内核中,随处可见,那为什么要用它们 ...

  5. javascript 树形菜单

    1. [代码][JavaScript]代码     var ME={ini:{i:true,d:{},d1:{},h:0,h1:0,h2:0},html:function(da,f){var s='& ...

  6. [推荐]Silverlight 2 开发者海报

    从Brad Abrams的Blog上看到了一张Silverlight 2开发者海报,非常酷,拿出来与大家分享. [JPG版本 5.8MB] [PNG版本 6.5MB] [TIF版本 19.9 MB] ...

  7. nginx最简单的网站配置:单主机+静态文件

    1.域名绑定主机:新建一个A记录,将www的子域名绑定到主机的ip上:2.主机的nginx设置监听端口,绑定域名,修改网站根目录路径和默认首选文件:/etc/nginx/conf.d 这个目录中新建一 ...

  8. [Java] public, private, protected

      同包不同类的成员 不同包中子类的成员 不同包非子类的成员 public √ √ √ protected √ √ × 默认 √ × × private × × ×

  9. Cascaded pose regression

    最近再看face alignment的相关文章,目前比较流行的算法都是基于(Cascaded pose regression,CPR)[1]的框架上做的,该算法之所以流行的原因是简单高效.CPR分为训 ...

  10. C++实现O(1)时间内删除链表结点

    /* * 删除链表节点.cpp * * Created on: 2018年4月13日 * Author: soyo */ #include<iostream> using namespac ...