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.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 |
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 ...
随机推荐
- OC——关于KVC
我们知道在C#中可以通过反射读写一个对象的属性,有时候这种方式特别方便,因为你可以利用字符串的方式去动态控制一个对象.其实由于ObjC的语言特性,你根部不必进行任何操作就可以进行属性的动态读写,这种方 ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- JavaScript遍历对象-总结一
原生JavaScript 遍历 1.for 循环遍历 let array1 = ['a','b','c']; for (let i = 0;i < array1.length;i++){ con ...
- MySQL高级查询(二)
EXISTS 和NOT EXISTS子查询 EXISTS子查询 语法: SELECT ……… FROM 表名 WHERE EXISTS (子查询); 例: SELECT `studentNo` A ...
- js中的||与&&用法
&&和||在JQuery源代码内尤为使用广泛,由网上找了些例子作为参考,对其用法研究了一下: &&: function a(){ alert("a" ...
- CentOSv6.8 修改防火墙配置、修改SSH端口
查看防火墙目前使用状况: service iptables status 修改防火墙配置: vi /etc/sysconfig/iptables 重启防火墙,让刚才修改的配置生效: service i ...
- (转)simhash算法原理及实现
simhash是google用来处理海量文本去重的算法. google出品,你懂的. simhash最牛逼的一点就是将一个文档,最后转换成一个64位的字节,暂且称之为特征字,然后判断重复只需要判断他们 ...
- PyCharm基本操作
1.1 PyCharm基本使用 视频学习连接地址:http://edu.51cto.com/course/9043.html 1.1.1 在Pycharm下为你的Python项目配置Python解释器 ...
- C#泛型基础知识点总结
1.0 什么是泛型 泛型是C#2.0和CLR(公共语言运行时)升级的一个新特性,泛型为.NET 框架引入了一个叫 type parameters(类型参数)的概念,type parameters 使 ...
- C# 使用NPOI 导出Excel
NPOI可以在没有安装Office的情况下对Word或Excel文档进行读写操作 下面介绍下NPOI操作Excel的方法 首先我们需要下载NPOI的程序集 下载地址 http://npoi.codep ...