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

9
12 7
24 9
30 5
41 9
80 7
50 87
22 9
45 1
50 7
0

Sample Output

243.06

#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-8) {
                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 >= 0) {
                return false;
            }
            v1 = l.B - l.A, v2 = A - l.A;
            c1 = v1 ^ v2;
            v2 = B - l.A;
            c2 = v1 ^ v2;
            if (c1 * c2 >= 0) {
                return false;
            }
            return true;
        }
    };
    int Graham(point * p, point * s, int n) {
        std::sort(p, p + n);
        int top, m;
        s[0] = p[0];
        s[1] = p[1];
        top = 1;
        for (int i = 2; i < n; i++) { //从前往后扫
            while (top > 0 && cp(p[i], s[top], s[top - 1]) >= 0) {
                top--;
            }
            s[++top] = p[i];
        }
        m = top;
        s[++top] = p[n - 2];
        for (int i = n - 3; i >= 0; i--) { //从后往前扫
            while (top > m && cp(p[i], s[top], s[top - 1]) >= 0) {
                top--;
            }
            s[++top] = p[i];
        }
        return top;
    }
}
//using namespace Geometry;
using namespace Geometry;
point p[200], s[200];
int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int n;
    while (scanf("%d", &n) != EOF) {
        if (n == 0) {
            break;
        }
        for (int i = 0; i < n; i++) {
            scanf("%lf%lf", &p[i].x, &p[i].y);
        }
        int cnt = 0;
        for (int i = 1; i < n; i++) //去掉重复的点
            if (fabs(p[i].x - p[cnt].x) > 1e-8 || fabs(p[i].y - p[cnt].y) > 1e-8) {
                p[++cnt] = p[i];
            }
        cnt++;
        if (cnt == 1) {
            printf("0.00\n");
            continue;
        } else if (cnt == 2) {
            printf("%.2lf\n", (p[1] - p[0]).mold());
            continue;
        }
        int top = Graham(p, s, cnt);
        double ans = 0;
        for (int i = 0; i < top - 1; i++) {
            ans += (s[i + 1] - s[i]).mold();
        }
        ans += (s[top - 1] - s[0]).mold();
        printf("%.2lf\n", ans);
    }
    return 0;
}

时间: 2024-10-06 00:54:02

Surround the Trees[HDU1392]的相关文章

HDU1392:Surround the Trees(凸包问题)

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7164    Accepted Submission(s): 2738 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

HDUJ 1392 Surround the Trees 凸包

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7203    Accepted Submission(s): 2752 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

hdu 1392 Surround the Trees (凸包)

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7043    Accepted Submission(s): 2688 Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

zoj 1453 Surround the Trees(凸包求周长)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=453 Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 r

Surround the Trees(凸包)

Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 291    Accepted Submission(s): 140   Problem Description There are a lot of trees in an area. A peasant wants to buy a rope to

HDU 1392 Surround the Trees

PS: 在求解两个点的时候就是两个点的距离,在这WA了一次. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <cmath> using namespace std; const int maxn = 110; struct point { int x, y; point(d

杭电 1392 Surround the Trees

经典凸包问题!!!! AC代码如下: #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<cstdio> using namespace std; struct H { double x,y; }trees[105]; bool cmp(H a,H b) { return a.x<b.x||(a.x==b.x&&a

hdu 1392 Surround the Trees(凸包果题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1392 Surround the Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7473    Accepted Submission(s): 2860 Problem Description There are a lot o

Surround the Trees

Surround the Trees: http://acm.hdu.edu.cn/showproblem.php?pid=1392 1 /* 2 2015.5 3 Surround the Trees 4 5 */ 6 7 #include<iostream> 8 #include<math.h> 9 #include<stdlib.h> 10 #define MAXD 110 11 using namespace std; 12 13 typedef struct