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 ...
随机推荐
- BZOJ2757 : [SCOI2012]Blinker的仰慕者
BZOJ AC900题纪念~~ 若K>0,则 设f[i][j]表示i位数字,积为j的数字的个数 g[i][j]表示i位数字,积为j的数字的和 DP+Hash预处理 查询时枚举LCP然后统计贡献 ...
- HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
- 【TYVJ】1463 - 智商问题(二分/分块)
http://tyvj.cn/Problem_Show.aspx?id=1463 二分的话是水题啊.. 为了学分块还是来写这题吧.. 二分: #include <cstdio> #incl ...
- eclipse远程调试Tomcat方法
1.Linux中配置tomcat在catalina.sh中添加如下CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address= ...
- JAVA图片处理--缩放,切割,类型转换
import java.io.*; import java.awt.*; import java.awt.image.*; import java.awt.Graphics; import java. ...
- FlyCapture2 Qt5 MinGW Configuration
Install FlyCatprue2 to the folder "C:\PointGreyResearch\" Add the following to the .pro fi ...
- thinkphp 代码执行
相关漏洞:http://loudong.360.cn/vul/info/id/2919 ThinkPHP 开启lite模式后,会加载ThinkPHP/Extend/Mode/Lite/Dispache ...
- 《GK101任意波形发生器》任意波文件格式说明
详见PDF 文档: http://files.cnblogs.com/xiaomagee/GK101%E4%BB%BB%E6%84%8F%E6%B3%A2%E6%95%B0%E6%8D%AE%E6%A ...
- Linux常用命令(持续更新中)
cd 目录名 :进入某个目录 ls :列出当前目录的内容 locate 文件名/目录名:寻找文件.目录 find 目录名1 -name 文件名/目录名2 :在目录1中寻找目录2 whereis 文件 ...
- 使用HIBERNATE的SQL查询并将结果集自动转换成POJO
在某些场合下,我们可能想使用HIBERNATE的框架提供的SQL查询接口,但是,由于实体没有做映射,HIBERNATE不能把结果集转换成你想要的List<POJO>,本文讨论如何在这种情况 ...