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
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)
 
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(凸包)的更多相关文章

  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 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

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

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

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

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

  5. hdu 1392 Surround the Trees 凸包裸题

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

  6. HDUJ 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 (凸包周长)

    题目链接:HDU 1392 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope ...

  8. HDU 1392 Surround the Trees(凸包*计算几何)

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

  9. hdu1392 Surround the Trees 凸包

    第一次做凸包,这道题要特殊考虑下,n=2时的情况,要除以二才行. 我是从最左边的点出发,每次取斜率最大的点,一直到最右边的点. 然后从最左边的点出发,每次取斜率最低的点,一直到最右边的点. #incl ...

  10. Surround the Trees(凸包求周长)

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

随机推荐

  1. [LeetCode] 231 Power of Two && 326 Power of Three && 342 Power of Four

    这三道题目都是一个意思,就是判断一个数是否为2/3/4的幂,这几道题里面有通用的方法,也有各自的方法,我会分别讨论讨论. 原题地址:231 Power of Two:https://leetcode. ...

  2. 浅析前端开发中的 MVC/MVP/MVVM 模式

    MVC,MVP和MVVM都是常见的软件架构设计模式(Architectural Pattern),它通过分离关注点来改进代码的组织方式.不同于设计模式(Design Pattern),只是为了解决一类 ...

  3. Corn Fields poj3254(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6081   Accepted: 3226 Descr ...

  4. combobox数据获取及使用总结

    写在前面 和队友完成工程实践项目过程中遇到combobox数据项加载失败的问题,我将记录下解决该问题中不断填坑的过程. 这是可以确定的填写正确的combobox内容 action也没有错误,Strut ...

  5. C++PrimerPlus第6版 第四章——复合类型

    1,复合类型主要包含:数组.结构.联合.枚举.类.指针.引用等. 2,数组.长度必须确定.即编译阶段,数组的长度就得确定好.所以只能使用常量(#define.const)声明数组长度.如果使用变量声明 ...

  6. 【JAVA零基础入门系列】Day3 Java基本数据类型

    前两篇已经将开发环境搭建完成,如果你已经按之前的教程按部就班的完成了部署,那么世界上最优秀的编程语言之一和世界上最优秀的IDE之一已经出现在你的电脑上(此处应有掌声),如果你还没入门,或者正在台阶上踱 ...

  7. FPGA多时钟处理应用

    FPGA项目设计中,通常会遇到多时钟处理.即一个PLL输出多个时钟,根据条件选择合适的时钟用作系统时钟.方案一: 外部晶振时钟进入PLL,由PLL输出多个时钟,MUX根据外部条件选择时钟输出做为系统使 ...

  8. Python实战之网络编程socket学习笔记及简单练习

    sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) 参数一:地址簇 socket.AF_INET IPv4(默认) socket.AF_IN ...

  9. Java高新技术 Myeclipse 介绍

      Java高新技术   Myeclipse 介绍 知识概述:              (1)Myeclipse开发工具介绍 (2)Myeclipse常用开发步骤详解               ...

  10. WPF: ShowDialog() 切换到其他应用窗口后,再切换回来无法让子窗口总在最上方

    按说ShowDialog()是模态窗口,应该在主窗口的上方,但是打开其他应用窗口再切换回来的时候,发现子窗口不见了,主窗口又不能操作. 另外子窗口设置成不在任务栏显示,只能通过Alt-Tab来切换到子 ...