P2742 模板二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
Posted bztminamoto
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P2742 模板二维凸包 / [USACO5.1]圈奶牛Fencing the Cows相关的知识,希望对你有一定的参考价值。
二维凸包的板子
//minamoto
#include<bits/stdc++.h>
#define rint register int
#define inf 0x3f3f3f3f
using namespace std;
const int N=10005;
struct node{
double x,y;
node(){}
node(double x,double y):x(x),y(y){}
inline bool operator <(const node &b)const{
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;
}
}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;
}
以上是关于P2742 模板二维凸包 / [USACO5.1]圈奶牛Fencing the Cows的主要内容,如果未能解决你的问题,请参考以下文章
P2742 [USACO5.1]圈奶牛Fencing the Cows /模板二维凸包(计算几何)(凸包)
P2742 模板二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
P2742 模板二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
P2742 [USACO5.1]圈奶牛Fencing the Cows /模板二维凸包