Surround the Trees

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6728    Accepted Submission(s):
2556

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
 
 
 
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std; struct node
{
int x,y;
};
node a[],stack1[]; double dis(node n1,node n2)//求距离
{
return (double)sqrt( (n1.x-n2.x)*(n1.x-n2.x)*1.0 + (n1.y-n2.y)*(n1.y-n2.y)*1.0 );
}
double cross(node a,node n1,node n2)// 为正时 "左转"
{
return (n1.x-a.x)*(n2.y-a.y) - (n1.y-a.y)*(n2.x-a.x);
}
bool cmp(node n1,node n2)// 叉乘越小的,越靠前
{
double k = cross(a[],n1,n2);
if( k>) return true;
else if( k== && dis(a[],n1)<dis(a[],n2))
return true;
else return false;
}
void Graham(int n)
{
int i,head;
double r=;
for(i=;i<n;i++)
if(a[i].x<a[].x ||(a[i].x==a[].x&&a[i].y<a[].y ) )
swap(a[],a[i]);
sort(a+,a+n,cmp); //排序
a[n]=a[]; //为了对最后一点的检验是否为满足凸包。cross(stack1[head-1],stack1[head],stack[i]);
stack1[]=a[];
stack1[]=a[];
stack1[]=a[];// 放入3个先
head=;
for(i=;i<=n;i++)
{
while( head>= && cross(stack1[head-],stack1[head],a[i])<= )head--;
// == 包含了重点和共线的情况。此题求周长,并没有关系。所以不加==,也是可以的。
stack1[++head]=a[i];
}
for(i=;i<head;i++) //不是<=. 因为 a[0]在 0 和 head 两个位置都出现了。
{
r=r+dis(stack1[i],stack1[i+]);
}
printf("%.2lf\n",r);
}
int main()
{
int i,n;
while(scanf("%d",&n)>)
{
if(n==)break;
for(i=;i<n;i++)
scanf("%d%d",&a[i].x,&a[i].y);//end input
if(n==)//特判
{
printf("0.00\n");
continue;
}
if(n==)//此题的特判
{
printf("%.2lf\n",dis(a[],a[]));
continue;
}
Graham(n);
}
return ;
}

hdu 1392 Surround the Trees 凸包模板的更多相关文章

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

  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. HDU 1392 Surround the Trees(凸包*计算几何)

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

  6. 计算几何(凸包模板):HDU 1392 Surround the Trees

    There are a lot of trees in an area. A peasant wants to buy a rope to surround all these trees. So a ...

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

随机推荐

  1. js加密解密 base64

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  2. Android实用代码片段

    有时候,需要一些小的功能,找到以后,就把它贴到了博客下面,作为留言,查找起来很不方便,所以就整理一下,方便自己和他人. 一.  获取系统版本号: 1 PackageInfo info = this.g ...

  3. HDU-1087-Super Jumping! Jumping! Jumping!(线性DP, 最大上升子列和)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  4. C#-WebForm-网页中Form表单中给回车绑定按钮

    WEB端:     <form id="form1" runat="server" defaultbutton="btnSearch" ...

  5. Python unittest第二篇:测试夹具

    关于测试夹具,我们知道,以类为对象的话,在python里对应的方法分别是test_isupper.test_upper, 那么以测试case为单位呢? 这时候,就要提到我们的setup()和tearD ...

  6. [BZOJ 5072][Lydsy1710月赛]小A的树

    传送门 \(\color{green}{solution}\) 嗯...其实我也不太会,所以大胆猜个结论吧(后来证了一下,然后放弃了...). 我们发现如果要使一个联通块的黑点数量为\(k\)的方案最 ...

  7. 【算法笔记】A1039 Course List for Student

    https://pintia.cn/problem-sets/994805342720868352/problems/994805447855292416 题意: 有N个学生,K节课.给出选择每门课的 ...

  8. 开源一个C# Class实现Openfire登陆、推出、消息发送,方便其他系统集成IM功能了

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  9. js if和switch,==和===

    今天改插件BoxScroll的时候,因为if里面的条件判断多于两个,于是立马想着改写switch.改到一半,忽然记起来JSHint等代码质量检测工具中的一个要求,用===替换==,不用不可靠的强制转型 ...

  10. 腾讯云技术专家卢萌凯手把手教你Demo一个人脸识别程序!

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文来自腾讯云技术沙龙,本次沙龙主题为Serverless架构开发与SCF部署实践 卢萌凯:毕业于东南大学,曾就职于华为,熟悉云行业解决方案 ...