Surround the Trees(凸包)
Surround the Trees |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 291 Accepted Submission(s): 140 |
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.
![]() |
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 |
Sample Output
243.06 |
Source
Asia 1997, Shanghai (Mainland China)
|
Recommend
Ignatius.L
|
/*
1,2的时候都需要特判。特别是n==2的时候,是返回1,2之间的距离,而不是返回两倍的距离
*/
#include<bits/stdc++.h>
using namespace std; const double eps = 1e-;
const double PI = acos(-1.0); int sgn(double x)
{
if(fabs(x) < eps)return ;
if(x < )return -;
else return ;
}
struct Point
{
double x,y;
Point(){}
Point(double _x,double _y)
{
x = _x;y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
//叉积
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
//点积
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
void input(){
scanf("%lf%lf",&x,&y);
}
};
struct Line {
Point s,e;
Line(){}
Line(Point _s,Point _e) {
s = _s; e = _e;
}
};
//*两点间距离
double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
}
/*
* 求凸包,Graham算法
* 点的编号0~n-1
* 返回凸包结果Stack[0~top-1]为凸包的编号
*/
const int MAXN = ;
Point List[MAXN];
int Stack[MAXN],top;
//相对于List[0]的极角排序
bool _cmp(Point p1,Point p2)
{
double tmp = (p1-List[])^(p2-List[]);
if(sgn(tmp) > )
return true;
else if(sgn(tmp) == && sgn(dist(p1,List[]) - dist(p2,List[])) <= )
return true;
else
return false;
}
void Graham(int n)
{
Point p0;
int k = ;
p0 = List[];
//找最下边的一个点
for(int i = ;i < n;i++)
{
if( (p0.y > List[i].y) || (p0.y == List[i].y && p0.x > List[i].x) )
{
p0 = List[i];
k = i;
}
}
swap(List[k],List[]);
sort(List+,List+n,_cmp);
if(n == )
{
top = ;
Stack[] = ;
return;
}
if(n == )
{
top = ;
Stack[] = ;
Stack[] = ;
return ;
}
Stack[] = ;
Stack[] = ;
top = ;
for(int i = ;i < n;i++)
{
while(top > && sgn((List[Stack[top-]]-List[Stack[top-]])^(List[i]-List[Stack[top-]])) <= )
top--;
Stack[top++] = i;
}
}
int n;
int main() {
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)!=EOF&&n){
for(int i=;i<n;i++){
List[i].input();
}
if(n==){
printf("0.00\n");
continue;
}else if(n==){
printf("%.2lf\n",dist(List[],List[]));
continue;
}
Graham(n);
double cur=;
for(int i=;i<top;i++){
cur+=dist(List[Stack[i%top]],List[Stack[(i+)%top]]);
}
printf("%.2lf\n",cur);
}
return ;
}
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 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU-1392 Surround the Trees,凸包入门!
Surround the Trees 此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点. 题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内. 思路:简单凸包入门题,凸包 ...
- HDU - 1392 Surround the Trees (凸包)
Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...
- 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 (凸包周长)
题目链接: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(凸包*计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...
- hdu1392 Surround the Trees 凸包
第一次做凸包,这道题要特殊考虑下,n=2时的情况,要除以二才行. 我是从最左边的点出发,每次取斜率最大的点,一直到最右边的点. 然后从最左边的点出发,每次取斜率最低的点,一直到最右边的点. #incl ...
- Surround the Trees(凸包求周长)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 线段树+RMQ问题第二弹
线段树+RMQ问题第二弹 上篇文章讲到了基于Sparse Table 解决 RMQ 问题,不知道大家还有没有印象,今天我们会从线段树的方法对 RMQ 问题再一次讨论. 正式介绍今天解决 RMQ 问题的 ...
- JAVA设计模式总结之六大设计原则
从今年的七月份开始学习设计模式到9月底,设计模式全部学完了,在学习期间,总共过了两篇:第一篇看完设计模式后,感觉只是脑子里面有印象但无法言语.于是决定在看一篇,到9月份第二篇设计模式总于看完了,这一篇 ...
- FPGA在电平接口领域的应用
电子技术的发展,产生了各种各样的电平接口. TTL电平: TTL电平信号之所以被广泛使用,原因是因为:通常我们采用二进制来表示数据.而且规定,+5V等价于逻辑"1",0V等价于逻辑 ...
- Python自学笔记-logging模块详解
简单将日志打印到屏幕: import logging logging.debug('debug message') logging.info('info message') logging.warni ...
- 合并Spark社区代码的正确姿势
原创文章,转载请保留出处 最近刚刚忙完Spark 2.2.0的性能测试及Bug修复,社区又要发布2.1.2了,国庆期间刚好有空,过了一遍2.1.2的相关JIRA,发现有不少重要修复2.2.0也能用上, ...
- 用$.getJSON() 和$.post()获取第三方数据做页面 ——惠品折页面(1)
用$.getJSON() 和$.post()获取第三方数据做页面 首页 index.html 页面 需要jquery 和 template-web js文件 可以直接在官网下载 中间导航条的固 ...
- 【特效】体验很好的导航hover效果移出恢复当前位置
很常见的一种导航的hover效果,鼠标放上后除了正常的hover,在移出整个导航后,会恢复当前栏目的特殊样式,分别有横向和纵向的导航.代码也比较简单,设置一个当前栏目的class,用index()找到 ...
- Xamarin.Forms 开发IOS、Android、UWP应用
C#语言特点,简单.快速.高效.本次我们通过C#以及Xaml来做移动开发. 1.开发工具visual studio 2015或visual studio 2017.当然visual studio 20 ...
- PDO详解
PDO扩展为PHP定义了一个访问数据库的轻量的,持久的接口.实现了PDO接口的每一种数据库驱动都能以正则扩展的形式把他们各自的特色表现出来.注意:利用PDO扩展本身并不能实现任何数据库函数.你必须使用 ...
- Mysql编写sql语句的小技巧
1.查询数据(保证查询性能) SELECT * 和 SELECT t.id , t.name:后者性能其实总体优于前者. 2.在查询的时候最好给表起个 别名,方便找到表中要查询的字段.执行sql的进行 ...