ACM学习历程—HDU1392 Surround the Trees(计算几何)
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
这个题目就是求凸包,然后求其凸包的周长。注意判断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(计算几何)的更多相关文章
- ACM学习历程—FZU2148 Moon Game(计算几何)
Moon Game Description Fat brother and Maze are playing a kind of special (hentai) game in the clearl ...
- ACM学习历程——UVA10112 Myacm Triangles(计算几何,多边形与点的包含关系)
Description Problem B: Myacm Triangles Problem B: Myacm Triangles Source file: triangle.{c, cpp, j ...
- HDU-1392 Surround the Trees,凸包入门!
Surround the Trees 此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点. 题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内. 思路:简单凸包入门题,凸包 ...
- ACM学习历程—FZU 2144 Shooting Game(计算几何 && 贪心 && 排序)
Description Fat brother and Maze are playing a kind of special (hentai) game in the playground. (May ...
- 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 ...
- ACM学习历程—BestCoder 2015百度之星资格赛1004 放盘子(策略 && 计算几何)
Problem Description 小度熊喜欢恶作剧.今天他向来访者们提出一个恶俗的游戏.他和来访者们轮流往一个正多边形内放盘子.最后放盘子的是获胜者,会赢得失败者的一个吻.玩了两次以后,小度熊发 ...
- ACM学习历程—HDU4720 Naive and Silly Muggles(计算几何)
Description Three wizards are doing a experiment. To avoid from bothering, a special magic is set ar ...
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
随机推荐
- spring中的事件 applicationevent 讲的确实不错
event,listener是observer模式一种体现,在spring 3.0.5中,已经可以使用annotation实现event和eventListner里. 我们以spring-webflo ...
- 设计一个线程安全的单例(Singleton)模式
在设计单例模式的时候.尽管非常easy设计出符合单例模式原则的类类型,可是考虑到垃圾回收机制以及线程安全性.须要我们思考的很多其它.有些设计尽管能够勉强满足项目要求,可是在进行多线程设计的时候.不考虑 ...
- 迁移EXT4
http://fanli7.net/a/JAVAbiancheng/ANT/20101003/43604.html 級別: 中級 Roderick W. Smith ,顧問和作家 2008 年6 月0 ...
- jQuery Validate(三)
这里,我们再说说radio.checkbox.select的验证方式. 1.用新版的写法进行验证. <!DOCTYPE html> <html> <head> &l ...
- 02-cookie案例-显示用户上次访问网站的时间
package cookie; import java.io.IOException;import java.io.PrintWriter;import java.util.Date; import ...
- coreseek中文搜索
coreseek的安装和使用 准备软件包 coreseek-3.2.14.tar.gz 其他汁源 coreseek中文索引-示例文件.zip sphinx配置文件详解.txt 1.安装组件 yum - ...
- thinkphp5 (最棒的php开源框架)
tp5的唯一可访问目录是public,即项目根目录: http://localhost/tp5/public/ 开发规范: 类库.函数文件统一以.php为后缀 类(命名和路径)和命名空间保持一致 类文 ...
- SSL:Ubuntu证书配置
CA证书的配置 Ubuntu上CA证书的配置可以通过工具ca-certificates来方便的进行.该工具默认是随Ubuntu安装的,如果没有可以通过下面的命令来安装: sudo apt-get in ...
- 您使用的是不受支持的命令行标记 chrome
检查 chrome://flags/#extensions-on-chrome-urls 是否开启 开启了的话就关掉检查 启动chrome的快捷方式是否在目标后有额外的参数 有就删了 在浏览器中输入c ...
- jxl java工具类,导出excel,导入数据库
1: 引入jxl jar 我使用的为maven管理, <!--Excel工具--> <dependency> <groupId>net.sourceforge.je ...