Luogu P2742 模板二维凸包
Posted wifimonster
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Luogu P2742 模板二维凸包相关的知识,希望对你有一定的参考价值。
https://www.cnblogs.com/zwfymqz/p/9264513.html#_label3_4
#include<bits/stdc++.h> using namespace std; const double eps=1e-8; int dcmp(double x) { if(fabs(x) < eps) return 0; return x < 0 ? -1 : 1; } #define Point Vector struct Vector{ double x,y; Vector(double x=0,double y=0):x(x),y(y){}; Vector operator + (const Vector &A) const{ return Vector(x+A.x,y+A.y); } Vector operator - (const Vector &A) const{ return Vector(x-A.x,y-A.y); } Vector operator * (const double &A) const{ return Vector(x*A,y*A); } Vector operator / (const double &A) const{ return Vector(x/A,y/A); } bool operator == (const Vector &A) const{ return dcmp(x-A.x)==0&&dcmp(y-A.y); } }p[10004],q[10004]; double PolarAngle(Vector A){ return atan2(A.y,A.x); } Vector rotate(Vector &A,double a){ return A=Vector(A.x*cos(a)-A.y*sin(a),A.x*sin(a)+A.y*cos(a)); } double Dot(Vector A,Vector B){ return A.x*B.x+A.y*B.y; } double Cross(Vector A,Vector B){ return A.x*B.y-A.y*B.x; } int n,t; bool operator < (const Point &x,const Point &y){ if(dcmp(x.x-y.x)==0) return x.y<y.y; else return x.x<y.x; } inline void Push(Point po){ while(Cross(q[t]-q[t-1],po-q[t-1])<0) t--; q[++t]=po; } inline void Andrew(){ q[0]=q[1]=p[1]; t=1; for(int i=2;i<=n;i++) Push(p[i]); for(int i=n-1;i;i--) Push(p[i]); } inline double dis(Point a,Point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } 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+n+1); Andrew(); double ans=0; for(int i=1;i<t;i++){ ans+=dis(q[i],q[i+1]); } printf("%.2lf",ans); }
以上是关于Luogu P2742 模板二维凸包的主要内容,如果未能解决你的问题,请参考以下文章
P2742 [USACO5.1]圈奶牛Fencing the Cows /模板二维凸包(计算几何)(凸包)
P2742 [USACO5.1]圈奶牛Fencing the Cows /模板二维凸包(计算几何)(凸包)
P2742 模板二维凸包 / [USACO5.1]圈奶牛Fencing the Cows
P2742 模板二维凸包 / [USACO5.1]圈奶牛Fencing the Cows