护城河的挖掘「USACO 2006」

题意

凸包模板,给定平面上点集,求包含所有点的凸包周长最小值。


思路

使用\(Graham\)扫描法解决。

考虑将最左下的点设为原点(事实上任意点均可作为原点),然后其余各点根据斜率排序。

对于每一个节点,我们考虑加入它是否会与已有的边点构成内凹,如果会,那么放弃已有边点。

显然我们可以通过维护一个单调栈完成该过程。

代码

#include <bits/stdc++.h>

using namespace std;

namespace StandardIO {

    template<typename T>inline void read (T &x) {
        x=0;T f=1;char c=getchar();
        for (; c<'0'||c>'9'; c=getchar()) if (c=='-') f=-1;
        for (; c>='0'&&c<='9'; c=getchar()) x=x*10+c-'0';
        x*=f;
    }

    template<typename T>inline void write (T x) {
        if (x<0) putchar('-'),x*=-1;
        if (x>=10) write(x/10);
        putchar(x%10+'0');
    }

}

using namespace StandardIO;

namespace Project {
    
    const int N=5005;
    const double eps=1e-9;
    
    int n,top;
    double ans;
    struct node {
        int x,y;
        double rad;
        node () {}
        node (int _x,int _y) : x(_x),y(_y) {}
        friend node operator + (const node &x,const node &y) {
            return node(x.x+y.x,x.y+y.y);
        }
        friend node operator - (const node &x,const node &y) {
            return node(x.x-y.x,x.y-y.y);
        }
        friend int operator * (const node &x,const node &y) {
            return x.x*y.y-x.y*y.x;
        }
        friend bool operator < (const node &x,const node &y) {
            return (x.x==y.x)?(x.y<y.y):(x.x<y.x);
        }
    } a[N],S[N];
    
    inline double dist (const node &x,const node &y) {
        return sqrt((y.x-x.x)*(y.x-x.x)+(y.y-x.y)*(y.y-x.y));
    }
    inline bool cmp (const node &x,const node &y) {
        double k=(x-a[1])*(y-a[1]);
        if (fabs(k)<=eps) return dist(x,a[1])<dist(y,a[1]);
        return k>eps;
    }
    inline double rad (node x,node y) {
        return atan2(y.y-x.y,y.x-x.x);
    }

    inline void MAIN () {
        read(n);
        for (register int i=1; i<=n; ++i) {
            read(a[i].x),read(a[i].y);
        }
        sort(a+1,a+n+1);
        sort(a+2,a+n+1,cmp);
        S[++top]=a[1],S[++top]=a[2];
        for (register int i=3; i<=n; ++i) {
            while ((S[top]-S[top-1])*(a[i]-S[top-1])<=0) --top;
            S[++top]=a[i];
        }
        S[top+1]=a[1];
        for (register int i=1; i<=top; ++i) {
            ans+=dist(S[i],S[i+1]);
        }
        printf("%.2lf",ans);
    }
    
}

int main () {
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    Project::MAIN();
}

猜你喜欢

转载自www.cnblogs.com/ilverene/p/11372811.html