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. FastDFS 安装与使用

    FastDFS 安装与使用 1. 什么是 FastDFS FastDFS是一个开源的高性能分布式文件系统(DFS). 它的主要功能包括:文件存储,文件同步和文件访问,以及高容量和负载平衡的设计. Fa ...

  2. Linux之解决命令行cat命令中文乱码

    临时解决cat中文乱码 cat test.txt | iconv -f GBK -t UTF-8

  3. Linux之E: 无法锁定管理目录(/var/lib/dpkg/),是否有其他进程正占用它?

    解决 ubantu系统中 E: 无法锁定管理目录(/var/lib/dpkg/),是否有其他进程正占用它? 的 问题. 1.解决办法: 当我们有的时候在使用apt-get install/update ...

  4. javascript举例介绍事件委托的典型使用场景

    在了解什么是DOM事件以及给DOM事件绑定监听器的几种方法后,我们来谈谈事件委托. 1. e.target 和 e.currentTarget 当我们给目标元素target 绑定一个事件监听器targ ...

  5. 架构师养成记--20.netty的tcp拆包粘包问题

    问题描述 比如要发ABC DEFG HIJK 这一串数据,其中ABC是一个包,DEFG是一个包,HIJK是一个包.由于TCP是基于流发送的,所以有可能出现ABCD EFGH 这种情况,那么ABC和D就 ...

  6. Python基础部分的疑惑解析(1)

    Python介绍: 是一种全能的语言,虽然执行效率低,但是开发效率高 现在也存在多种版本,IPYTHON,JPYTHON,但最重要的是CPYTHON,其他都是作用于各种语言的粘合剂版本,执行效率低,C ...

  7. jsp页面用struts2标签展示List<Object>类型的数据

    今天遇到一个问题,一个List<Object>类型的数据,是直接从sql查出来的数据,要在前端展示,原来的方法不知道为什么不能展示,后来找了好久,找到了一个靠谱的方法,记录一下 <s ...

  8. jenkins backup and migration

    service jenkins stop 压缩包:tar -czvf /tmp/xx.tar.gz --exclude=“workspace” --exclude=“.m2" --exclu ...

  9. Mac下配置git环境和客户端SourceTree+Git常用命令大全(Mac 10.12)

    前言: 如果不想折腾,直接下载GitHub桌面端,高度集成git,不需要学习git的任何命令. https://desktop.github.com/ 一.配置git环境 1.上官网https://g ...

  10. @using (Html.BeginForm())和@{Html.BeginForm();}@{Html.EndForm();}对比

    这样写报错 <body>    @using (Html.BeginForm())    {         form主体1    }    @{Html.BeginForm();}    ...