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 ...
随机推荐
- jsonp的作用
jsonp(即JSON with padding),也就是json填充. 背景: json格式的数据对比xml格式的数据,性能上已经有了很大的提升.但是json可以被本地执行仍然会导致几个重要的性能问 ...
- HTML 5语义化标签
HTML 5的革新之一:语义化标签一节元素标签. 在HTML 5出来之前,我们用div来表示页面章节,但是这些div都没有实际意义.(即使我们用css样式的id和class形容这块内容的意义).这些标 ...
- Java通过接口实现匿名类的实例
package com.chase.test; /** * 通过接口实现匿名类的实例 * * @author Chase * * @date 2013-10-18 下午04:28:17 * * @ve ...
- URAL - 1114-Boxes (分步乘法原理)
题意; 给你n个盘子,A个红球,B个黑球,放的时候没有限制,可以不放,可以放一个红球,可以放一个黑球,也可以两个同时放,可以有剩余的球. 求一共有多少放法. 思路: 可以利用分步乘法原理,红球和黑球是 ...
- springboot框架嵌入netty
1.pom.xml添加依赖 <dependency> <groupId>io.netty</groupId> <artifactId>netty-all ...
- 【hihocoder 1499】A Box of Coins
[题目链接]:http://hihocoder.com/problemset/problem/1499 [题意] [题解] 贪心,模拟; 从左往右对于每一列; 如果上下两个格子; ① 有一个格子超过了 ...
- [bzoj3209]花神的数论题_数位dp
花神的数论题 bzoj-3209 题目大意:sum(i)表示i的二进制表示中1的个数,求$\prod\limits_{i=1}^n sum(i)$ 注释:$1\le n\le 10^{15}$. 想法 ...
- 洛谷——P1060 开心的金明
https://www.luogu.org/problem/show?pid=1060#sub 题目描述 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间他自己专用的很宽敞的房间.更让他高兴的 ...
- 关于使用JetbrainsCrack破解idea
转载自:https://blog.csdn.net/android_ztz/article/details/73762603 一.前言 IDEA在业界被公认为JAVA最优秀的开发工具,但是它不像ecl ...
- dubbo协议参考手册(转)
原文链接:http://wely.iteye.com/blog/2331332 协议参考手册 (+) (#) 推荐使用Dubbo协议 性能测试报告各协议的性能情况,请参见:性能测试报告 (+) dub ...