Surround the Trees[HDU1392]
Surround the Trees
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11811 Accepted Submission(s): 4577
Problem 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.
Output
The minimal length of the rope. The precision should be 10^-2.
Sample Input
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0
Sample Output
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
//#include <algorithm>
namespace Geometry {
class point {
public:
double x, y;
point() {}
point(const point &p): x(p.x), y(p.y) {}
point(double a, double b): x(a), y(b) {}
point operator + (const point & p)const {
point ret;
ret.x = x + p.x, ret.y = y + p.y;
return ret;
}
point operator - (const point & p)const {
point ret;
ret.x = x - p.x, ret.y = y - p.y;
return ret;
}
//dot product
double operator * (const point & p)const {
return x * p.x + y * p.y;
}
//cross product
double operator ^ (const point & p)const {
return x * p.y - p.x * y;
}
bool operator < (const point & p)const {
if (fabs(x - p.x) < 1e-) {
return y < p.y;
}
return x < p.x;
}
double mold() {
return sqrt(x * x + y * y);
}
};
double cp(point a, point b, point o) {
return (a - o) ^ (b - o);
}
class line {
public:
point A, B;
line() {}
line(point a, point b): A(a), B(b) {}
bool IsLineCrossed(const line &l)const {
point v1, v2;
double c1, c2;
v1 = B - A, v2 = l.A - A;
c1 = v1 ^ v2;
v2 = l.B - A;
c2 = v1 ^ v2;
if (c1 * c2 >= ) {
return false;
}
v1 = l.B - l.A, v2 = A - l.A;
c1 = v1 ^ v2;
v2 = B - l.A;
c2 = v1 ^ v2;
if (c1 * c2 >= ) {
return false;
}
return true;
}
};
int Graham(point * p, point * s, int n) {
std::sort(p, p + n);
int top, m;
s[] = p[];
s[] = p[];
top = ;
for (int i = ; i < n; i++) { //从前往后扫
while (top > && cp(p[i], s[top], s[top - ]) >= ) {
top--;
}
s[++top] = p[i];
}
m = top;
s[++top] = p[n - ];
for (int i = n - ; i >= ; i--) { //从后往前扫
while (top > m && cp(p[i], s[top], s[top - ]) >= ) {
top--;
}
s[++top] = p[i];
}
return top;
}
}
//using namespace Geometry;
using namespace Geometry;
point p[], s[];
int main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
#endif
int n;
while (scanf("%d", &n) != EOF) {
if (n == ) {
break;
}
for (int i = ; i < n; i++) {
scanf("%lf%lf", &p[i].x, &p[i].y);
}
int cnt = ;
for (int i = ; i < n; i++) //去掉重复的点
if (fabs(p[i].x - p[cnt].x) > 1e- || fabs(p[i].y - p[cnt].y) > 1e-) {
p[++cnt] = p[i];
}
cnt++;
if (cnt == ) {
printf("0.00\n");
continue;
} else if (cnt == ) {
printf("%.2lf\n", (p[] - p[]).mold());
continue;
}
int top = Graham(p, s, cnt);
double ans = ;
for (int i = ; i < top - ; i++) {
ans += (s[i + ] - s[i]).mold();
}
ans += (s[top - ] - s[]).mold();
printf("%.2lf\n", ans);
}
return ;
}
Surround the Trees[HDU1392]的更多相关文章
- 【计算几何初步-凸包-Jarvis步进法。】【HDU1392】Surround the Trees
[科普]什么是BestCoder?如何参加? Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU-1392 Surround the Trees,凸包入门!
Surround the Trees 此题讨论区里大喊有坑,原谅我没有仔细读题还跳过了坑点. 题意:平面上有n棵树,选一些树用绳子围成一个包围圈,使得所有的树都在这个圈内. 思路:简单凸包入门题,凸包 ...
- HDU1392:Surround the Trees(凸包问题)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Surround the Trees(凸包求周长)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Surround the Trees(凸包)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1392 Surround the Trees 凸包裸题
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1392 Surround the Trees 凸包模板
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDUJ 1392 Surround the Trees 凸包
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1392:Surround the Trees(计算几何,求凸包周长)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- eslint推荐编码规范和airbnb推荐编码规范
Eslint规范 for 循环禁止使用无限循环(这个非默认推荐) // bad for (var i = 0; i < 10; i--) { } for (var i = 10; i >= ...
- python 爬取妹子
爬取妹子图片 网址:https://www.mzitu.com/jiepai/ 2019-06-13 环境WIN10 1903 python 3.7.3 个人习惯先在IDLE中进行调试 import ...
- android全屏下的输入框未跟随软键盘弹起问题
最近开发中遇到,全屏模式下输入框在底部不会跟随软键盘弹起.于是网上搜索了解决的方案.大致找到了两种方案. 第一种 定义好此类 public class SoftKeyBoardListener { p ...
- 什么是hashMap,初始长度,高并发死锁,java8 hashMap做的性能提升
问题1:HashM安排的初始长度,为什么? 初始长度是 16,每次扩展或者是手动初始化,长度必须是 2的幂. 因为: index = HashCode(Key) & (length - 1), ...
- Mysql学习总结(39)——30条MySql语句优化技巧
1.应尽量避免在 where 子句中使用!=或<>操作符,否则将引擎放弃使用索引而进行全表扫描. 2.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉 ...
- 炒了8年的概念,到底该如何理解DevOps这个词?
什么是DevOps及误区 DevOps概念从2009年提出已有8个年头.可是在8年前的那个时候,为什么DevOps没有迅速走红呢?即便是在2006年Amazon发布了ECS,微软在2008年和2010 ...
- 0223实战:MySQL Sending data导致查询很慢的问题详细分析
转自博客http://blog.csdn.net/yunhua_lee/article/details/8573621 [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据 ...
- 关于double类型数字相加位数发生变化的问题
因为计算机内部存贮本身的缺陷,导致double类型的数字相加.得到的结果有非常多位,比方 774.23 750.0 2638.66 4162.889999999999 看到这个是不是非常晕 当然 ...
- HDU 1466
经典DP,这样的递推确实有点难. 把所有直线分成两组,可以知道 m条直线的交点方案数 =(m-r)条平行线与r条直线交叉的交点数 + r条直线本身的交点方案 亦就是 =(m-r)*r+r条之间本身 ...
- 多版本号并发控制(MVCC)在实际项目中的应用
近期项目中遇到了一个分布式系统的并发控制问题.该问题能够抽象为:某分布式系统由一个数据中心D和若干业务处理中心L1,L2 - Ln组成:D本质上是一个key-value存储,它对外提供基于HTTP协议 ...