题目链接: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模板)的更多相关文章

  1. HDU1392:Surround the Trees(凸包问题)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. Surround the Trees(凸包)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  3. HDU 1392 Surround the Trees(凸包入门)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  4. Surround the Trees(凸包求周长)

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  5. Surround the Trees[HDU1392]

    Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. zoj 1453 Surround the Trees(凸包求周长)

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=453 Time Limit: 2 Seconds      Memory ...

  7. 题解报告: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 ...

  8. HDU 1392 Surround the Trees(凸包)题解

    题意:给一堆二维的点,问你最少用多少距离能把这些点都围起来 思路: 凸包: 我们先找到所有点中最左下角的点p1,这个点绝对在凸包上.接下来对剩余点按照相对p1的角度升序排序,角度一样按距离升序排序.因 ...

  9. HDU 1392 Surround the Trees 构造凸包

    又是一道模板题 #include <iostream> #include <cstring> #include <cstdlib> #include <cst ...

随机推荐

  1. java工程包的命名(-dev.jar,-javadoc.jar,jar)

    1.-javadoc.jar 这个应该是一个doc包,就是API,相当于帮助文档,用来说明类.方法.静态变量 都是干什么的. 2.-dev.jar,.jar都有可能包含源码,也可能都不包含

  2. lucene 3.0.2 搜索

    1.lucene 词频 转载:http://mxdxm.iteye.com/blog/989031 lucene in action作为action系列,确实坚持了其实用性的特色.全书花了很大的篇幅来 ...

  3. System-Defined Device Setup Classes Available to Vendors

    https://msdn.microsoft.com/en-us/library/windows/hardware/ff553426(v=vs.85).aspx

  4. PHP json_decode 函数解析 json 结果为 NULL 的解决方法

    在做网站 CMS 模块时,对于模块内容 content 字段,保存的是 json 格式的字符串,所以在后台进行模块内容的编辑操作 ( 取出保存的数据 ) 时,需要用到 json_decode() 函数 ...

  5. PHP 使用 GET 传递数组变量

    PHP 代码: <?php print_r($_GET['tag_name']); URL: http://127.0.0.16/get.php?tag_name[]=送货快&tag_n ...

  6. js 模拟ajax方式提交数据

    html页面 <script>function LocaluploadCallback(msg) {     document.getElementById("f_localup ...

  7. 为什么我的联想打印机M7450F换完墨粉之后打印机显示请更换墨粉盒?这是我的墨盒第一次灌粉·、

    需要打印机清零,可以网上查到的,要不就去买颗芯片换上关掉机器 →开机的同时按住功能按扭不松手开机→进入维修模式→翻到84功能项→按OK→用下翻键找到PROCESS CHECK→按OK 按扭→关机→正常 ...

  8. 20145235 《Java程序设计》第一次实验报告

    实验一Java开发环境的熟悉 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验知识点 1.JVM.JRE.JDK的安装位置与区 ...

  9. nginx下增加模块

    1.使用nginx -V确定nginx的编译参数2.下载nginx源码3.下载nginx的扩展模块(此处为memcache模块)4.进入nginx源码目录5../configure  --prefix ...

  10. 来到这里,我放弃了多少- UI基础-疯狂猜图,我们都疯狂了-

    小问题也要问 学习最重要的是 自律 我昨天晚上3点睡的, 这两天一点也没睡 0.99*0.99  每天差一点 日积月累就很多了 关键字,在字典里查一下,在类里面查查 瑞详博客下载器 跑步后精神多了,白 ...