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

Source

Asia 1997, Shanghai (Mainland China)


思路

这就是找最小凸包并求其周长的过程,可以采用Graham算法,具体步骤如下:

  • 读入一系列坐标并找到y坐标最小的坐标设置为\(p_0\)(如果x坐标相同就找x最小的)
  • 对除了\(p_0\)以外的点按照逆时针以相对p0的极角排序,相同极角的点则保留一个离\(p_0\)最远的点
  • 设置一个栈,前三个候选点先入栈,接下来让剩下的点一一入栈,去掉所有非左转的情况,由此,栈里的点就是凸包的点

具体看注释

代码

#include<bits/stdc++.h>
using namespace std;
struct point
{
double x,y;
}a[110]; double dis(point a,point b)
{
return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
} double crossMult(point a, point n1, point n2)
{
return (n1.x-a.x)*(n2.y-a.y) - (n1.y-a.y)*(n2.x-a.x);
} //叉积,以a为基 bool cmp(point n1, point n2)
{
double k = crossMult(a[1],n1,n2); //叉积
if( k>0 ) return true; //叉积>0则说明a在b的顺时针方向上
else
{
if( k==0 && dis(a[1],n1) < dis(a[1],n2) ) //叉积为0说明a和b在同一条直线上,且更远
return true;
}
return false;
} void sortByAngel(int n)
{
point tmp;
int k = 1;
for(int i=2; i<=n; i++)
{
if( a[i].y < a[k].y || a[i].y == a[k].y && a[i].x < a[k].x)
k = i;
} //找出p0
tmp = a[1];
a[1] = a[k];
a[k] = tmp;
sort(a+2, a+n+1,cmp); //对除了p0以外的点逆时针以相对p0的极角进行排序
} double Graham(int n)
{
sortByAngel(n) ;
point stack[110];
double sum = 0.0;
a[n+1] = a[1];
stack[1] = a[1]; stack[2] = a[2]; stack[3] = a[3];
int top = 3;//指向栈顶
for(int i=4;i<=n+1;i++) //这里是遍历到n+1,因为要回到最初的点
{
while( (crossMult(stack[top-1], stack[top], a[i])<=0) &&
top >= 3) top--; //保证是左转而且栈里面至少要有2个点(后面才能做叉积)
top++;
stack[top] = a[i];
}
for(int i=1;i<top;i++)//这里i<top即可,因为后面要访问的是stack[i],stack[i+1]
{
sum += dis(stack[i],stack[i+1]);
}
return sum;
} int main()
{
int n;
while(cin>>n)
{
if(0==n) break;
for(int i=1;i<=n;i++)
cin >> a[i].x >> a[i].y; if(1==n)
cout << "0.00" << endl;
else if(2==n)
printf("%.2lf\n",dis(a[1],a[2]));
else
{
double ans = Graham(n);
printf("%.2lf\n",ans);
}
}
return 0;
}

Hdoj 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 凸包裸题

    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. HDUJ 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. 题解报告:hdu 1392 Surround the Trees(凸包入门)

    Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...

  9. HDU 1392 Surround the Trees(凸包)题解

    题意:给一堆二维的点,问你最少用多少距离能把这些点都围起来 思路: 凸包: 我们先找到所有点中最左下角的点p1,这个点绝对在凸包上.接下来对剩余点按照相对p1的角度升序排序,角度一样按距离升序排序.因 ...

随机推荐

  1. python os模块详解

    一.Python os模块(Linux环境) 1.1 执行shell命令 os.system('cmd') 执行命令不保存结果 os.popen('command') 执行后返回结果,使用.read( ...

  2. 3proxy.cfg 配置文件解析

    最新配置文件的man文档所在位置: /程序目录/doc/html/man3/3proxy.cfg.3.html 官网: https://3proxy.ru/ Download 3proxy tiny ...

  3. 练习MD5加密jar包编写

    简介 参数签名可以保证开发的者的信息被冒用后,信息不会被泄露和受损.原因在于接入者和提供者都会对每一次的接口访问进行签名和验证. 签名sign的方式是目前比较常用的方式. 第1步:接入者把需求访问的接 ...

  4. opencv自带fast_math.hpp

    cvRound cvFloor cvCeil cvIsNaN cvIsInf

  5. hive权限配置

    基于CDH5.x的Hive权限配置 1.打开权限控制,默认是没有限制的 set hive.security.authorization.enabled=true; 2.配置默认权限 hive.secu ...

  6. Spring 基于XML配置

    基于XML的配置 对于基于XML的配置,Spring 1.0的配置文件采用DTD格式,Spring2.0以后采用Schema格式,后者让不同类型的配罝拥有了自己的命名空间,使得配置文件更具扩展性.此外 ...

  7. vue-router路由懒加载(解决vue项目首次加载慢)

    懒加载:----------------------------------------------------? 也叫延迟加载,即在需要的时候进行加载,随用随载. 为什么需要懒加载? 像vue这种单 ...

  8. MHA高可用及读写分离

    一.MHA简介 二.工作流程 三.MHA架构图 四.MHA工具介绍 五.基于GTID的主从复制 六.部署MHA 七.配置VIP漂移 八.配置binlog-server 九.MySQL中间件Atlas

  9. spring程序打包war,直接通过-jar启动,并指定spring.profiles.active参数控制多环境配置

    备注:spring boot有内嵌tomcat,jar项目可以用java -jar命令启动,war包也可以,且可以直接指定spring.profiles.active参数控制多环境配置 直接指定传参, ...

  10. Bootstrap之图片展示界面Demo

    代码:(使用模板引擎freemarker) <!DOCTYPE html> <html> <head> <title>图片</title> ...