P2742 【模板】二维凸包 / [USACO5.1]圈奶牛Fencing the Cows

传送门

二维凸包的板子,抄yyb大佬的

//minamoto
#include<bits/stdc++.h>
#define rint register int
#define inf 0x3f3f3f3f
using namespace std;
const int N=10005;
struct node{double x,y;}p[N],st[N];
int n,k,top;double ans;
inline bool cmp(node a,node b){
    double A=atan2((a.y-p[1].y),(a.x-p[1].x));
    double B=atan2((b.y-p[1].y),(b.x-p[1].x));
    return A!=B?A<B:a.x<b.x;
}
inline double cross(node a,node b,node c){return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);}
inline double dis(node a,node b){return sqrt((b.y-a.y)*(b.y-a.y)+(b.x-a.x)*(b.x-a.x));}
int main(){
//	freopen("testdata.in","r",stdin);
    scanf("%d",&n),p[0]={inf,inf};
    for(rint i=1;i<=n;++i){
        scanf("%lf%lf",&p[i].x,&p[i].y);
        if(p[0].y>p[i].y||(p[0].y==p[i].y&&p[i].x<p[0].x))p[0]=p[i],k=i;
    }
    swap(p[k],p[1]),sort(p+2,p+n+1,cmp);
    st[0]=p[1],st[1]=p[2],top=1;
    for(rint i=3;i<=n;++i){
        while(top&&cross(st[top-1],p[i],st[top])>=0)--top;
        st[++top]=p[i];
    }
    st[top+1]=p[1];for(rint i=0;i<=top;++i)ans+=dis(st[i],st[i+1]);
    printf("%.2lf
",ans);return 0;
}
原文地址:https://www.cnblogs.com/bztMinamoto/p/9989228.html