HDU 1392 Surround the Trees【凸包 | 模板】

题目链接

题目描述:

凸包模板题


凸包:

凸包 (Convex hull) 问题:

给定一些点,求能把所有这些点包含在内的面积最小的多边形。可以想象有一个很大的橡皮箍,它把所有的点都箍在里面,在橡皮箍收紧之后,绕着最外围的点形成的多边形就是凸包。

求凸包的算法 (Andrew算法):

算法做两次扫描,先从最左边的点沿“下凸包”扫描到最右边,再从最右边的点沿“上凸包”扫描到最左边,“上凸包”和“下凸包”合起来就是完整的凸包。

步骤:
  1. 把所有点按照横坐标 x 从小到大进行排序,如果 x 相同,按 y 从小到大排序,并删除重复的点,得到序列 p 0 , p 1 , p 2 , . . . , p n {p_0, p_1, p_2, ..., p_n} p0,p1,p2,...,pn
  2. 从左到右扫描所有点,求“下凸包”。 p 0 p_0 p0 一定在凸包上,它是凸包最左边的顶点,从 p 0 p_0 p0 开始,依次检查 p 0 , p 1 , p 2 , . . . , p n {p_0, p_1, p_2, ..., p_n} p0,p1,p2,...,pn,扩展出“下凸包”。判断的依据是:如果新点在凸包“前进”方向的左边,说明在“下凸包”上,把它加入到凸包;如果在右边,说明拐弯了,删除最近加入凸包的点。继续这个过程,直到检查完所有点。拐弯方向用叉积判断即可。
  3. 从右到左重新扫描所有点,求“上凸包”。和求“下凸包”的过程类似,最右边的点 p n p_n pn 一定在凸包上。
复杂度:

排序 O ( n l o g 2 n ) O(nlog_2n) O(nlog2n),然后扫描 O ( n ) O(n) O(n) 次得到凸包,算法总复杂度为 O ( n l o g 2 n ) O(nlog_2n) O(nlog2n)


AC Codes:

#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
//#include <unordered_set>
//#include <unordered_map>
#include <deque>
#include <list>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
//#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
//cout << fixed << setprecision(2);
//cout << setw(2);
const int N = 2e5 + 6, M = 1e9 + 7, INF = 0x3f3f3f3f;
const double eps = 1e-8;

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

struct Point {
    double x, y;
    Point() {};
    Point(double a, double b) : x(a), y(b) {};
    Point operator + (Point B) {return Point(x + B.x, y + B.y);}
    Point operator - (Point B) {return Point(x - B.x, y - B.y);}
    bool operator == (const Point &other) const {return sgn(x - other.x) == 0 && sgn(y - other.y) == 0;}
    bool operator < (const Point other) const {
        if (sgn(x - other.x) != 0) return sgn(x - other.x) < 0;
        return sgn(y - other.y) < 0;
    }
};
typedef Point Vector;
double Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;}
double Distance(Point A, Point B) {return hypot(A.x - B.x, A.y - B.y);}

int main() {
    //freopen("/Users/xumingfei/Desktop/ACM/test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int n;
    while (cin >> n && n) {
        vector<Point> p(n);
        for (int i = 0; i < n; i++)
            cin >> p[i].x >> p[i].y;
        sort(p.begin(), p.end());
        n = (int) (unique(p.begin(), p.end()) - p.begin());
        vector<Point> ch(n + 1);
        int d = 0;
        for (int i = 0; i < n; i++) {
            while (d > 1 && sgn(Cross(p[i] - ch[d - 2], ch[d - 1] - ch[d - 2])) >= 0)
                d--;
            ch[d++] = p[i];
        } 
        int u = d;
        for (int i = n - 2; i >= 0; i--) {
            while (u > d && sgn(Cross(p[i] - ch[u - 2], ch[u - 1] - ch[u - 2])) >= 0)
                u--;
            ch[u++] = p[i];
        }
        if (n > 1) u--;
        double ans = 0;
        if (u == 1) {
            ans = 0;
        } else if (u == 2) {
            ans = Distance(ch[0], ch[1]);
        } else {
            for (int i = 0; i < u; i++) ans += Distance(ch[i], ch[(i + 1) % u]);
        }
        cout << fixed << setprecision(2) << ans << '\n';
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值