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(double x=0, double y=0):x(x),y(y) {}
};
point operator - (point A, point B) {
    return point(A.x-B.x, A.y-B.y);
}

double Dist(point A, point B) {
    double x = (A.x-B.x)*(A.x-B.x);
    double y = (A.y-B.y)*(A.y-B.y);
    return sqrt(x+y);
}
point ch[maxn];
vector<point> v;
int n;

double Cross(point A, point B) {
    return A.x*B.y - A.y*B.x;
}

bool cmp(point a, point b) {
    if(a.x!=b.x) return a.x < b.x;
    else return a.y<b.y;
}
int ConvexHull() {
    int m = 0;
    for(int i = 0; i < n; i++) {
        while(m>1 && Cross(ch[m-1]-ch[m-2], v[i]-ch[m-2])<=0)
            m--;
        ch[m++] = v[i];
    }
    int k = m;
    for(int i = n-2; i >= 0; i--) {
        while(m>k && Cross(ch[m-1]-ch[m-2], v[i]-ch[m-2])<=0)
            m--;
        ch[m++] = v[i];
    }
    if(n>1) m--;
    return m;
}

int main()
{
    point t;
    while(scanf("%d", &n) && n) {
        v.clear();
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &t.x, &t.y);
            v.push_back(t);
        }
        sort(v.begin(), v.end(), cmp);
        int m = ConvexHull();
        double res = 0;
        if(m==2) {
            res = Dist(ch[0], ch[1]);
            printf("%.2lf\n", res);  // res*2 WA.
            continue;
        }
        for(int i = 0; i < m-1; i++) {
            res += Dist(ch[i], ch[i+1]);
        }
        res += Dist(ch[m-1], ch[0]);
        printf("%.2lf\n", res);
    }
    return 0;
}

HDU 1392 Surround the Trees,码迷,mamicode.com

时间: 2024-09-30 15:48:26

HDU 1392 Surround the Trees的相关文章

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

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

HDU - 1392 Surround the Trees (凸包)

Surround the Trees:http://acm.hdu.edu.cn/showproblem.php?pid=1392 题意: 在给定点中找到凸包,计算这个凸包的周长. 思路: 这道题找出凸包上的点后,s数组中就是按顺序的点,累加一下距离就是周长了. #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdli

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

HDU 1392.Surround the Trees【凸包(求凸包周长)】【5月10】

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

HDU 1392 Surround the Trees(几何 凸包模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1392 题目大意: 二维平面给定n个点,用一条最短的绳子将所有的点都围在里面,求绳子的长度. 解题思路: 凸包的模板.凸包有很多的算法.这里用Adrew. 注意这几组测试数据 1 1 1 3 0 0 1 0 2 0 输出数据 0.00 2.00 1 #include<cmath> 2 #include<cstdio> 3 #include<algorithm> 4 using name

HDU 1392 Surround the Trees (Graham求凸包周长)

题目链接 题意 : 让你找出最小的凸包周长 . 思路 : 用Graham求出凸包,然后对每条边求长即可. Graham详解 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <math.h> 5 #include <algorithm> 6 7 using namespace std ; 8 9 struct point 10 { 11 int

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

杭电 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