Description

There are a lot of trees in an area. A peasant wants to 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.

Input

The input contains one or more data sets. At first line 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.       
Sample Input

9

12 7

24 9

30 5

41 9

80 7

50 87

22 9

45 1

50 7

0

Sample Output

243.06

这个题目就是求凸包,然后求其凸包的周长。注意判断n为1和n为2的特殊情况。

 
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define INF 0x3fffffff using namespace std; struct point
{
int x, y;
}; point p[105], s[105]; bool mult(point sp, point ep, point op)
{
return(sp.x - op.x) * (ep.y - op.y) >= (ep.x - op.x) * (sp.y - op.y);
} bool operator < (const point &p1, const point &p2)
{
return p1.y < p2.y || (p1.y == p2.y && p1.x < p2.x);
} int graham(point *p, int n, point *s)
{
int len, top = 1;
sort(p, p + n);
if (n == 0) return 0;
s[0] = p[0];
if (n == 1) return 1;
s[1] = p[1];
if (n == 2) return 2;
s[2] = p[2];
for (int i = 2; i < n; ++i)
{
while (top && mult(p[i], s[top], s[top -1])) top--;
s[++top] = p[i];
}
len = top; s[++top] = p[n-2];
for (int i = n - 3; i >= 0; --i)
{
while (top != len && mult(p[i], s[top], s[top-1])) top--;
s[++top] = p[i];
}
return top;
} int main()
{
//freopen ("test.txt", "r", stdin);
int n;
while (scanf ("%d", &n) != EOF && n != 0)
{
for (int i = 0; i < n; ++i)
{
scanf ("%d%d", &p[i].x, &p[i].y);
}
int len = graham(p, n, s);
double ans = 0;
long long temp;
if (len == 1)
{
printf("0.00\n");
continue;
}
if (len == 2)
{
temp = (s[0].x - s[len-1].x) * (s[0].x - s[len-1].x);
temp += (s[0].y - s[len-1].y) * (s[0].y - s[len-1].y);
ans += sqrt(temp);
printf ("%.2lf\n", ans);
continue;
}
for (int i = 0; i < len; ++i)
{
if (i == 0)
{
temp = (s[0].x - s[len-1].x) * (s[0].x - s[len-1].x);
temp += (s[0].y - s[len-1].y) * (s[0].y - s[len-1].y);
ans += sqrt(temp);
}
else
{
temp = (s[i].x - s[i-1].x) * (s[i].x - s[i-1].x);
temp += (s[i].y - s[i-1].y) * (s[i].y - s[i-1].y);
ans += sqrt(temp);
}
}
printf ("%.2lf\n", ans);
}
return 0;
}

ACM学习历程—HDU1392 Surround the Trees(计算几何)的更多相关文章

  1. ACM学习历程—FZU2148 Moon Game(计算几何)

    Moon Game Description Fat brother and Maze are playing a kind of special (hentai) game in the clearl ...

  2. ACM学习历程——UVA10112 Myacm Triangles(计算几何,多边形与点的包含关系)

    Description   Problem B: Myacm Triangles Problem B: Myacm Triangles Source file: triangle.{c, cpp, j ...

  3. HDU-1392 Surround the Trees,凸包入门!

    Surround the Trees 此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点. 题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内. 思路:简单凸包入门题,凸包 ...

  4. ACM学习历程—FZU 2144 Shooting Game(计算几何 && 贪心 && 排序)

    Description Fat brother and Maze are playing a kind of special (hentai) game in the playground. (May ...

  5. ACM学习历程—FZU 2140 Forever 0.5(计算几何 && 构造)

    Description   Given an integer N, your task is to judge whether there exist N points in the plane su ...

  6. ACM学习历程—BestCoder 2015百度之星资格赛1004 放盘子(策略 && 计算几何)

    Problem Description 小度熊喜欢恶作剧.今天他向来访者们提出一个恶俗的游戏.他和来访者们轮流往一个正多边形内放盘子.最后放盘子的是获胜者,会赢得失败者的一个吻.玩了两次以后,小度熊发 ...

  7. ACM学习历程—HDU4720 Naive and Silly Muggles(计算几何)

    Description Three wizards are doing a experiment. To avoid from bothering, a special magic is set ar ...

  8. 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始

    以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告

  9. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

随机推荐

  1. Excel COM组件使用的注意事项和一些权限问题(转载)

    1.实例化Excel的COM组件的时候,不要直接调用类,要用Microsoft提供的接口 原来的写法:Excel.ApplicationClass excelApp = new Excel.Appli ...

  2. 简述什么是ajax、javascript、json、Jquery?

    什么是Javascript? 基于对象.解释型.事件驱动.脚本语言.封装在<script>标签中使用.弱类型.与浏览器交互执行 什么 是Ajax? ajax是一种编程模式.在客户端与服务器 ...

  3. 开源监控系统Prometheus介绍

    前言 Prometheus是CNCF的一个开源项目,Google BorgMon监控系统的开源版本,是一个系统和服务的监控系统.周期性采集metrics指标,匹配规则和展示结果,以及触发某些条件的告警 ...

  4. 数据结构---python---表

    一.list的基本实现技术 在数据结构中,如果用python实现线性表,无疑要提到list,list是一种元素个数可变的线性表(而tuple是不变的表,不支持改变其内部状态的任何操作,其他与list性 ...

  5. 从symbol link和hard link 到 unlink函数的一点记录

    之前一直对Linux的文件类型中的 “l” 类型的了解不是很深入,最近经过“圣经”指点,略知一二,在此先记录一下,以便以后查阅,之后会对文件和目录.文件I/O这部分再扩充. 首先需明确,Unix在查阅 ...

  6. 【BZOJ3319】黑白树 并查集

    [BZOJ3319]黑白树 Description 给定一棵树,边的颜色为黑或白,初始时全部为白色.维护两个操作:1.查询u到根路径上的第一条黑色边的标号.2.将u到v    路径上的所有边的颜色设为 ...

  7. Darwin做直播时对ReflectorSession引用数的控制

    在之前的博客中,我们提到了如何用Darwin&live555实现直播过程,那么更进一步,当直播结束时,我们需要关闭所有正在收看的客户端,并且delete转发会话ReflectorSession ...

  8. ubuntu 安装codeblocks

    本文转载于:http://blog.csdn.net/i_fuqiang/article/details/9749225 1.安装gcc: sudo apt-get install build-ess ...

  9. django框架小技巧

    带命名空间的URL名字 多应用中路由定义,采用命名空间,防止冲突 url(r'^polls/', include('polls.urls', namespace="polls")) ...

  10. HibernateTemplate方法的使用

    1.查询帖子(Post)为例 查找所有的帖子 public List<Post> findPosts() { String hql = "from Post p left joi ...