HDU 1392 Surround the Trees (Graham求凸包周长)
题意 : 让你找出最小的凸包周长 。
思路 : 用Graham求出凸包,然后对每条边求长即可。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <algorithm> using namespace std ; struct point
{
int x,y ;
}p[],p1[];
int n ; double dis(point a,point b)
{
return sqrt(double((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))) ;
}
double cross(point a,point b,point c)
{
return (a.x-c.x)*(b.y-c.y) - (a.y-c.y)*(b.x-c.x) ;
}
bool cmp(point a,point b)
{
if(cross(a,b,p[]) > || cross(a,b,p[]) == && dis(a,p[]) < dis(b,p[]))
return true ;
return false ;
}
void Graham()
{
int cnt;
sort(p+,p+n,cmp) ;
p[n] = p[] ;
p1[] = p[] ;
p1[] = p[] ;
cnt = ;
for(int i = ; i <= n ; i++)
{
while(cnt >= && cross(p1[cnt-],p1[cnt],p[i]) <= ) cnt -- ;
p1[ ++cnt] = p[i] ;
}
double sum = 0.0 ;
for(int i = ; i < cnt ; i++)
{
sum += dis(p1[i],p1[i+]) ;
//printf("*%.2lf*\n",sum) ;
}
printf("%.2lf\n",sum) ;
}
int main()
{
while(~scanf("%d",&n) && n)
{
int pos = ;
for(int i = ; i < n ; i++)
{
scanf("%d %d",&p[i].x,&p[i].y) ;
if(p[pos].y > p[i].y || (p[pos].y == p[i].y && p[i].x < p[pos].x))
pos = i ;
}
if(n == ) {puts("0.00");continue ;}
if(n == )
{
printf("%.2lf\n",dis(p[],p[])) ;
continue ;
}
swap(p[pos],p[]) ;
//printf("%d %d\n",p[0].x,p[0].y) ;
Graham() ;
}
return ;
}
HDU 1392 Surround the Trees (Graham求凸包周长)的更多相关文章
- HDU 1392 Surround the Trees(几何 凸包模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1392 题目大意: 二维平面给定n个点,用一条最短的绳子将所有的点都围在里面,求绳子的长度. 解题思路: 凸包的模 ...
- 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 (凸包周长)
题目链接: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(凸包*计算几何)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 这里介绍一种求凸包的算法:Graham.(相对于其它人的解释可能会有一些出入,但大体都属于这个算 ...
- hdu 1348:Wall(计算几何,求凸包周长)
Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- POJ 1113 Wall(Graham求凸包周长)
题目链接 题意 : 求凸包周长+一个完整的圆周长. 因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆. 思路 : 求出凸包来,然后加上圆的周长 #include <stdi ...
- 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 (凸包)
Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出 ...
随机推荐
- oracle 查询谁在用undo
SELECT TO_CHAR(s.sid)||','||TO_CHAR(s.serial#) sid_serial,NVL(s.username, 'None') orauser,s.program, ...
- android开发中系统自带语音模块的使用
android开发中系统自带语音模块的使用需求:项目中需要添加语音搜索模块,增加用户体验解决过程:在网上搜到语音搜索例子,参考网上代码,加入到了自己的项目,完成产品要求.这个问题很好解决,网上能找到很 ...
- Go原子计数
通过原子计数可以在多线程情况下,对同一个数值进行加减操作,一般用于状态同步. 先看代码: package main import "fmt" import "time&q ...
- iOS关于打包出错
运行没问题,有可能是自动打包编译脚本的存在,删除掉即可.
- [原创]flexslider 中文文档 使用教程 参数手册
要改前人用的flexslider功能,但苦于找不到详细的文档教程,折磨了好久……(所以我才说不爱乱用插件) 为了福利下之后也苦于这个问题的人,我整理总结了下我找到的一些东西.可能没那么完善正确,欢迎在 ...
- ASP.NET&AJAX&JSON - 动态读取数据
因为之前帮WM组做了一个delivery的dashboard,大概用了3周的时间,.net也忘了差不多了,ajax和highchart表也是现学的,蛮费劲!总算也搞出来了.发帖纪录一下. 1. 前台A ...
- 2. VS使用---HelloWorld
摘要: ------------------------------------------------------------------------------------- 1. VS2010里 ...
- [rsync]——rsync文件同步和备份
实验环境 (1) Rsync服务器:10.0.10.158 (2) Rsync客户端:10.0.10.173 Rsync服务器端的配置 1. 安装xinetd和rsync # yum install ...
- js之Function原型
在ECMAScript中,Function(函数)类型实际上是对象.每个函数都是Function类型的实例,而且都与其他引用类型一样具有属性和方法.由于函数是对象,因此函数名实际上也是一个指向函数对象 ...
- 搭建SpringMVC+MyBatis开发框架一
大部分 Java 应用都是 Web 应用,展现层是 Web 应用不可忽略的重要环节.Spring 为展现层提供了一个优秀的 Web 框架—— Spring MVC.和众多其他 Web 框架一样,它基于 ...