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},然后取集合里 ...
随机推荐
- 最短路 uva12661 Funny Car Racing
传送门:点击打开链接 题意:给你有向图,每条边呈周期性开放,即开放a时间,再关闭b时间.再开放a时间以此类推 假设时间不足以穿过这条路则不能走.你能够在节点等待时间,问从s走到t所须要的最小时间 细致 ...
- wifi认证Portal开发系列(二):FreeRadius的安装和测试、关联Mysql
注:本次安装是基于FreeRadius 3版本进行安装配置的,在配置Mysql的过程中,与2版本有些不同.操作系统是CentOS 7 一.准备工作 工具的安装 #安装rz.sz命令用于文件上传 yum ...
- web翻译——插件
很多时候,可能我们web项目中需要的只是机械式的翻译,并不需要什么利用xml或者js json等等实现逼真翻译,那样工作量太大.这时候可能你就需要这几款小工具来帮助你.当然,如果 对翻译或者你的项目外 ...
- 向oracle中插入date时,持久层sql怎么写???
public class EmpDao { public void addEmp(Emp emp) throws SQLException { QueryRunner runner = new Que ...
- 07 redi sorder set结构及命令详解
zadd key score1 value1 score2 value2 .. 添加元素 redis 127.0.0.1:6379> zadd stu 18 lily 19 hmm 20 lil ...
- matlab biplot 符号的困惑
在matlab中做Principal component Analysis 时,常要用biplot 函数来画图,表示原分量与主分量(principal component)之间的关系,以及原始观察数据 ...
- SQL 经验总结
总结日常工作中使用SQL遇到的坑和技巧,一是加深印象,二是方便回顾.会不定期更新. 坑 1.多表联查时要使用表名,如果两个表的有列名相同的情况你没有加别名,那么sql编译器就不知道使用哪个列.这时进行 ...
- 九度OJ 1058:反序输出 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8454 解决:3042 题目描述: 输入任意4个字符(如:abcd), 并按反序输出(如:dcba) 输入: 题目可能包含多组用例,每组用例 ...
- iOS 跳转到Appstore的链接及二维码
1.应用内部跳转到Appstore 1.跳转到应用详情 [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"it ...
- 点聚-weboffice 6.0 (一)
WebOffice是一款由北京点聚信息技术有限公司提供的完全免费(商业用途也免费)且功能强大的在线Word/excel/wps编辑辅助控件,可以实现:1.在线编辑Word.Excel.PPT.WPS. ...