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 ...
随机推荐
- Jmeter Plugins----- Transactions per Second 配置项
Jmeter Plugins---version 0.5.5 官方解释: Transactions per Second since 0.3.0 This graph shows the number ...
- 在Myeclipse中查看android源码就是这么easy
在开发android 时不能查看源码必是很不爽的一件事,看过网上一些文章后(都是2.0以前的版本,跟我的2.2最新版本的配置是不一样的)不过还是给了我启示,通过配置终于可以在myeclipse中查看源 ...
- CentOS7打开关闭防火墙与端口
http://www.javahelp.com.cn/h-nd-747.html#_np=153_1707
- java简单工厂设计模式
一.基本定义 /* *简单工厂设计模式: *文字描述理解: * 简单工厂模式属于类的创建型模式,又叫做静态工厂方法模式. * 通过专门定义一个类来负责创建其它类的实例,被创建的实例通常 * 都具有共同 ...
- 对称矩阵与压缩存储算法(java实现)
package 数组和矩阵; public class DuiChenJuZhenYaSuo { int[] arr; int order; //矩阵的阶数 int n; //压缩后一位数组的个数 p ...
- C#-WebForm-ajax状态保持
cookies: ashx端赋值: context.Response.Cookies["Username"].Value = ""; 后台端加载: Respon ...
- docker版redmine安装部署
数据库准备 docker run -d --name some-postgres -e POSTGRES_PASSWORD=secret -e POSTGRES_USER=redmine postgr ...
- Visual Studio各个版本对应关系
- Git、Github、码云 笔记汇总
从本地恢复码云的项目 把本地项目同步到码云 CBoard 基于0.4.1的旧版本的分支修改合并到0.4.2新版本里面 通过git命令行把一个分支的其中一个commit(提交)合并到另外一个分支里面去
- SSAS Tabular模式中关系设置不支持直接设置多对多?
在网上文档发现一篇文章 微软BI 之SSAS 系列 - 多维数据集维度用法之一 引用维度 Referenced Dimension 有涉及到SSAS模型的关系设置 但是本人的基于表格模型的 没有关系可 ...