首页 > 其他 > 详细

凸包(BZOJ1069)

时间:2016-12-23 22:57:46      阅读:247      评论:0      收藏:0      [点我收藏+]

顶点一定在凸包上,我们枚举对角线,观察到固定一个点后,随着另一个点的增加,剩下两个点的最优位置一定是单调的,于是就得到了一个优秀的O(n^2)做法。

#include <cstdio>
#include <algorithm>

const int N = 2005;
int n,r,p1,p2,q[N];
double ans;
struct nd {
    double x,y;
    bool operator < (const nd &b) const {return x == b.x ? y < b.y : x < b.x;}
    nd operator - (const nd &b) const {return (nd){x-b.x,y-b.y};}
    double operator * (const nd &b) const {return x*b.y-y*b.x;}
}a[N];
double cj(int x, int y, int z) {return (a[x]-a[y])*(a[z]-a[y]);}

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%lf%lf", &a[i].x, &a[i].y);
    std::sort(a+1, a+1+n);
    for(int i = 1; i <= n; i++) {
        while(r > 1 && cj(q[r], q[r-1], i) >= 0) r--;
        q[++r] = i;
    }
    int rr = r;
    for(int i = n-1; i >= 1; i--) {
        while(r > rr && cj(q[r], q[r-1], i) >= 0) r--;
        q[++r] = i;
    }
    for(int i = 1; i < r; i++) printf("%.0f %.0f\n", a[q[i]].x, a[q[i]].y);
    for(int i = 1; i < r; i++) {
        int p1 = i+1, p2 = i+3;
        for(int j = i+2; j < r-1; j++) {
            while(p1 < j-1 && cj(q[j],q[i],q[p1]) <= cj(q[j],q[i],q[p1+1])) p1++;
            while(p2 <= j || (p2 < r-1 && cj(q[p2],q[i],q[j]) <= cj(q[p2+1],q[i],q[j]))) p2++;
            ans = std::max(ans, cj(q[j],q[i],q[p1])+cj(q[p2],q[i],q[j]));
        }
    }
    printf("%.3f", ans/2);
    return 0;
}

凸包(BZOJ1069)

原文:http://www.cnblogs.com/juruolty/p/6216118.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!