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},然后取集合里 ...
随机推荐
- 转:HDMI介绍与流程
HDMI介绍与流程 HDMI,全称为(High Definition Multimedia Interface)高清多媒体接口,主要用于传输高清音视频信号. HDMI引脚: HDMI有A,B,C, ...
- Redis系列-存储篇list主要操作函数小结(转)
在总结list之前,先要弄明白几个跟list相关的概念: 列表:一个从左到右的队列,个人理解更类似于一个栈,常规模式下,先进列表的元素,后出. 表头元素:列表最左端第一个元素. 表尾元素:列表最右端的 ...
- 安卓ADT离线安装
http://jingyan.baidu.com/article/3aed632e66858770108091bf.html
- pycharm连git和gitee
http://www.cnblogs.com/feixuelove1009/p/5955332.html https://www.58jb.com/html/171.html
- FPGA学习记录 - Quartus II 未使用管脚设置为三态输入
未使用管脚设置为三态输入 Assignments -> Device 或双击器件
- Circling Round Treasures CodeForces - 375C
C. Circling Round Treasures time limit per test 1 second memory limit per test 256 megabytes input s ...
- EasyNVR H5无插件摄像机直播解决方案前端解析之:videojs的使用
video.js的基本使用方法 一.videojs的初始化加载 videojs初始化加载分为两中 1.标签式加载 在引入videojs加载文件的前提下,可以在video标签中添加属性值"da ...
- python数据分析之:数据加载,存储与文件格式
前面介绍了numpy和pandas的数据计算功能.但是这些数据都是我们自己手动输入构造的.如果不能将数据自动导入到python中,那么这些计算也没有什么意义.这一章将介绍数据如何加载以及存储. 首先来 ...
- Linux就该这么学--Shell脚本条件语句(二)
1.for条件语句 先读取多个不同的变量值,然后逐一执行同一组命令. 从列表文件中读取主机地址,逐个测试是否在线. 从ipadds.txt中读取主机地址后赋值给HLIST变量后逐个ping列表中的主机 ...
- 关于left join遇到where就不管用的问题
今天做了个存储过程,需要的功能是查询所有人的得分,有人没分就给零分,显而易见这里用左外连接是没有问题的, 但是在连接完之后有个根据时间筛选功能,于是我加了where条件判断,这时候没有想到的事情发生了 ...