0%

HDU 1392 Surround the Trees (凸包周长)

题目链接:HDU 1392

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

1
2
3
4
5
6
7
8
9
10
11
9 
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0

Sample Output

1
243.06

Source

Asia 1997, Shanghai (Mainland China)

Solution

题意:

给定若干个点的坐标,求凸包周长。

思路

凸包

凸包入门模板题,我用的是 $Andrew$ 算法

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int maxn = 1e5 + 10;

int n;
struct Point {
double x, y;
Point() {}
Point(double a, double b) : x(a), y(b) {}
bool operator<(const Point &b) const {
if (x < b.x) return 1;
if (x > b.x) return 0;
return y < b.y;
}
Point operator-(const Point &b) {
return Point(x - b.x, y - b.y);
}
} p[maxn], stk[maxn];
typedef Point Vec;

int sgn(double x) {
if (fabs(x) <= eps)
return 0;
return x > 0 ? 1 : -1;
}

double dist(Point a, Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

double cross(Vec a, Vec b) {
return a.x * b.y - a.y * b.x;
}

int Andrew() {
sort(p + 1, p + 1 + n);
int len = 0;
for (int i = 1; i <= n; ++i) {
while (len > 1 && sgn(cross(stk[len] - stk[len - 1], p[i] - stk[len - 1])) == -1) {
len--;
}
stk[++len] = p[i];
}
int k = len;
for (int i = n - 1; i >= 1; --i) {
while (len > k && sgn(cross(stk[len] - stk[len - 1], p[i] - stk[len - 1])) == -1) {
len--;
}
stk[++len] = p[i];
}
return len;
}

int main() {
while(cin >> n && n) {
for (int i = 1; i <= n; ++i) {
scanf("%lf%lf", &p[i].x, &p[i].y);
}
int t = Andrew();
double ans = 0;
for (int i = 1; i < t; i++) {
ans += dist(stk[i], stk[i + 1]);
}
printf("%.2lf\n", n == 2 ? ans / 2 : ans);
}
return 0;
}

欢迎关注我的其它发布渠道