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. Controller层返回字符串

    刚开始练习,有时候想让Controller层返回一个字符串,但是他却去寻找这个字符串名字的jsp页面,结果肯定会是404的,研究了一会才明白过来,如果Controller需要返回一个值的话,需要再方法 ...

  2. 小程序踩坑之不同屏幕下动态改变translate值

    案例还原 小程序做一个进度条,可以通过拽转控制进度 那么肯定有一个进度条,不过小程序自己会做适配宽高 6s下这个div的width 是250 6splus就是276 但是问题来了,我拖拽用的是tran ...

  3. centOS7 安装nginx+php+mysql

    nginx安装 本文是介绍使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g++ 开发库之类的需要提前装好. 安装make: yum -y install gcc automa ...

  4. iOS界面-仿网易新闻左侧抽屉式交互 续(添加新闻内容页和评论页手势)

     本文转载至  http://blog.csdn.net/totogo2010/article/details/8637430       1.介绍 有的博友看了上篇博文iOS界面-仿网易新闻左侧抽屉 ...

  5. 发送邮件 Email(java实现)

    //发送邮件 private static void sendMail(String mail, String mailContext) { try { //获取文本中html的内容 并动态替换并显示 ...

  6. 九度OJ 1171:C翻转 (矩阵计算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4649 解决:1530 题目描述: 首先输入一个5 * 5的数组,然后输入一行,这一行有四个数,前两个代表操作类型,后两个数x y代表需操作 ...

  7. To discount or not to discount in reinforcement learning: A case study comparing R learning and Q learning

    https://www.cs.cmu.edu/afs/cs/project/jair/pub/volume4/kaelbling96a-html/node26.html [平均-打折奖励] Schwa ...

  8. vue+vuex构建单页应用

    基本 构建工具: webpack 语言: ES6 分号:行首分号规则(行尾不加分好, [ , ( , / , + , - 开头时在行首加分号) 配套设施: webpack 全家桶, vue 全家桶 项 ...

  9. 查看SqlServer安装的log文件

    SqlServer安装时产生的log被保存在这个目录下: "%programfiles%\Microsoft SQL Server\[SQL_VERSION]\Setup Bootstrap ...

  10. matlab实战中一些重要的函数总结

    这段时间看了一些大型的matlabproject文件(如:faster r-cnn),对于project中常常要用到的一些函数进行一个总结. 1.路径问题. 这主要涵括文件路径的包括和组合. curd ...