hdu 1392 Surround the Trees 凸包模板
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
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.
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.
10^-2.
#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 凸包模板的更多相关文章
- 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 (凸包)
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 ...
- 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(凸包*计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...
- 计算几何(凸包模板):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 ...
- 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 ...
- HDUJ 1392 Surround the Trees 凸包
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- jzoj3511
设f[i][j][k] 表示第i行状态为j i+1行将要被放为状态k的最优解 每次枚举这行和上一行的状态来dfs,注意细节 不合法的状态会直接被赋值成为inf
- Java多线程原理及Thread类的使用
一.进程与线程的区别 1.进程是应用程序在内存总分配的空间.(正在运行中的程序) 2.线程是进程中负责程序执行的执行单元.执行路径. 3.一个进程中至少有一个线程在负责进程的运行. 4.一个进程中有多 ...
- hadoop1.0.4运行程序出现“Java heap Space”错误
根据虾皮博客中教程,成功搭建了一个12台电脑的Hadoop云平台,而且成功运行了软件自带的wordcount程序,处理10M数据. 但是当程序处理40M时候,却出错了.出错提示“Java Heap S ...
- 一篇文章搞懂Linux安全!
Linux是开放源代码的免费正版软件,同时也是因为较之微软的Windows NT网络操作系统而言,Linux系统具有更好的稳定性.效率性和安全性. 在Internet/Intranet的大量应用中,网 ...
- Class文件分析
java源码和class文件 源码: package classloader; public class TestCase { private int value; public int test() ...
- Spring Boot Cookbook 中文笔记
Spring Boot Cookbook 一.Spring Boot 入门 Spring Boot的自动配置.Command-line Runner RESTful by Spring Boot wi ...
- windows 域的安装方法
前面的博客中我们知道了 Windows AD域的升级,下面我谈谈Windows域的安装和卸载. 卸载AD域 配置备份AD域 安装子域 删除子域(必须在根域管理员模式下删除,否则无法删除) 删除命令 导 ...
- Oracle数据库学习(二):Oracle Linux下oracle、ogg的挂载与参数配置
准备工作:打开虚拟机端的Oracle Linux Server 6.9的系统,然后使用root用户登录.打开终端界面,输入ifconfig -a查看IP地址. 然后在本地打开XShell软件使用以下命 ...
- 2019.04.07 第三次训练 【WHU校赛】
A: (模拟退火+点到线段最短距离) https://blog.csdn.net/Link_Ray/article/details/89173222 B:✅ C: (线段树+二分) https://b ...
- e的理解
1. e是一个重要的常数,但是我一直不知道,它的真正含义是什么. 它不像π.大家都知道,π代表了圆的周长与直径之比3.14159,可是如果我问你,e代表了什么.你能回答吗? 维基百科说: " ...