Surround the Trees---hdu1392(凸包GraHam模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392
题意:有n棵树,每棵树有一个坐标,想用一些绳子把这些树包含起来,求需要绳子的长度;
就是求凸包的周长的,把凸包各边的长度加起来就好了;注意n<=2的情况,运用GraHam算法,时间复杂度是O(nlogn);
GraHam算法的过程:
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
const double eps = 1e-;
const double PI = acos(-);
const int N = ; struct point
{
double x, y;
point(double x=, double y=) : x(x), y(y){}
friend point operator - (const point& p1, const point& p2)
{
return point(p1.x-p2.x, p1.y-p2.y);
}
friend double operator ^ (const point& p1, const point& p2)
{
return p1.x*p2.y - p1.y*p2.x;
}
}p[N], res[N]; double Dist(point p1, point p2)
{
double dx = p1.x - p2.x, dy = p1.y - p2.y;
return sqrt(dx*dx + dy*dy);
}
bool cmp1(point p1, point p2)
{
if(p1.y == p2.y)
return p1.x < p2.x;
return p1.y < p2.y;
}
bool cmp2(point p1, point p2)///极角排序;若极角相同,距离近的在前面;
{
double k = (p1-p[])^(p2-p[]);
if( k>eps || (fabs(k)<eps && Dist(p1, p[]) < Dist(p2, p[]) ))
return ;
return ;
}
int Graham(int n)///返回凸包的点的个数;
{
res[] = p[];if(n == ) return ;
res[] = p[];if(n == ) return ;
int top = ;
for(int i=; i<n; i++)
{
while(top && ((res[top]-res[top-])^(p[i]-res[top-])) <= ) top--;
res[++top] = p[i];
}
return top+;
} int main()
{
int n;
while(scanf("%d", &n), n)
{
for(int i=; i<n; i++)
scanf("%lf %lf", &p[i].x, &p[i].y); if(n == || n == )
{
printf("0\n");
continue;
}
if(n == )
{
printf("%.2f\n", Dist(p[], p[]));
continue;
} sort(p, p+n, cmp1);///p[0]为最下方靠左的点;
sort(p+, p+n, cmp2);///以p[0]为基点,按叉积进行排序; int cnt = Graham(n);///求凸包的顶点个数cnt+1,保存在res中,下标从0开始; double ans = Dist(res[], res[cnt-]);
for(int i=; i<cnt; i++)
ans += Dist(res[i], res[i-]); printf("%.2f\n", ans);
}
return ;
}
Surround the Trees---hdu1392(凸包GraHam模板)的更多相关文章
- HDU1392:Surround the Trees(凸包问题)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 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 ...
- Surround the Trees(凸包求周长)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Surround the Trees[HDU1392]
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- zoj 1453 Surround the Trees(凸包求周长)
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=453 Time Limit: 2 Seconds Memory ...
- 题解报告:hdu 1392 Surround the Trees(凸包入门)
Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to surround a ...
- HDU 1392 Surround the Trees(凸包)题解
题意:给一堆二维的点,问你最少用多少距离能把这些点都围起来 思路: 凸包: 我们先找到所有点中最左下角的点p1,这个点绝对在凸包上.接下来对剩余点按照相对p1的角度升序排序,角度一样按距离升序排序.因 ...
- HDU 1392 Surround the Trees 构造凸包
又是一道模板题 #include <iostream> #include <cstring> #include <cstdlib> #include <cst ...
随机推荐
- Spark Streaming实时计算框架介绍
随着大数据的发展,人们对大数据的处理要求也越来越高,原有的批处理框架MapReduce适合离线计算,却无法满足实时性要求较高的业务,如实时推荐.用户行为分析等. Spark Streaming是建立在 ...
- css expression
概述 css expression(css表达式)又称Dynamic properties(动态属性)是早期微软DHTML的产物,以其可以在Css中定义表达式(公式)来达到建立元素间属性之间的联系等作 ...
- BZOJ 1067 & Interval_Tree
1067: [SCOI2007]降雨量 Time Limit: 1 Sec Memory Limit: 162 MB Submit: 3099 Solved: 800 Description 我们常常 ...
- 【壁纸自动换】自动下载、更换壁纸(Bing壁纸)--XinBSBingWallPaper[2.7更新]
XinBSBingWallPaper主要功能: 1.支持自动下载Bing壁纸.Netbian壁纸.美国国家地理杂志图片. 2.自动搜索.下载多国Bing首页壁纸. 3.支持定时自动更换桌面壁纸. 4. ...
- fork和execve
fork函数在新的子进程中运行相同的程序,新的子进程是父进程的一个复制品. execve函数在当前进程的上下文中加载并运行一个新的程序.它会覆盖当前进程的地址空间,但并没有创建一个新的进程.新的程序仍 ...
- linux下查看一个进程的启动时间和运行时间
使用 ps命令 :#ps -A -opid,stime,etime,args ps -A -opid,stime,etime,args 结果: root:src# ps -A -opid,stime, ...
- Javascript - 数组去重复
这里我使用的场景是将表单中所有的input的值塞入数组中,然后通过去除重复的值.如果数组的长度和原数组的长度一致,说明没有重复,如果不一致(少于)则报错 //通过$.unique对数组进行“去重”,再 ...
- 内核Makefile编写
对于大部分内核模块或设备驱动的开发者和使用者来说,最常接触到的就是各层目录下基于kbuild架构的kbuild Makefile文件.主要部分有:1.目标定义,目标定义就是用来定义哪些内容要做为模块编 ...
- dpi,ppi,dip,dp,px和sp
一 基本概念 1. dpi (dots per inch)每英寸多少点:ppi( Pixel per inch),每英寸像素数.针对显示器的设计时,dpi=ppi. 2. dip (device in ...
- 【新产品发布】EVC8003 磁耦隔离型USB转全功能RS-232
============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...