码迷,mamicode.com
首页 > 其他好文 > 详细

【模板】二维凸包

时间:2019-03-31 19:33:43      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:c++   www   main   point   turn   uri   http   tmp   typename   

题目地址

https://www.luogu.org/problemnew/show/P2742

代码

#include <bits/stdc++.h>
#define db double 
#define N 10005
using namespace std;
template <typename T> T sqr(T x) {return x * x;}
struct Point {
    db x, y;
}p[N], s[N];
db dist(Point p1, Point p2) {
    return sqrt(sqr(p1.x - p2.x) + sqr(p1.y - p2.y));
}
bool cmp(Point p1, Point p2) {
    return (p1.x == p2.x)? (p1.y < p2.y): (p1.x < p2.x);
}
db cross(Point p1, Point p2, Point p3, Point p4) {
    return (p2.x - p1.x) * (p4.y - p3.y) - (p4.x - p3.x) * (p2.y - p1.y);
}
int n, tot;
db ans = 0;
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i ++) scanf("%lf%lf", &p[i].x, &p[i].y);
    sort(p + 1, p + 1 + n, cmp);
    tot = 0;
    for (int i = 1; i <= n; i ++) {
        while (tot > 1 && cross(s[tot], s[tot - 1], s[tot], p[i]) <= 0) -- tot;
        s[++ tot] = p[i];
    }
    int tmp = tot;
    for (int i = n - 1; i >= 1; i --) {
        while (tot > tmp && cross(s[tot], s[tot - 1], s[tot], p[i]) <= 0) -- tot;
        s[++ tot] = p[i];
    }
    for (int i = 2; i <= tot; i ++) ans += dist(s[i], s[i - 1]);
    printf("%.2lf\n", ans);
    return 0;
}

【模板】二维凸包

标签:c++   www   main   point   turn   uri   http   tmp   typename   

原文地址:https://www.cnblogs.com/chhokmah/p/10632630.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!