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)==0;
    }
}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);
}

 

posted @ 2019-01-26 09:52  WiFiMonster  阅读(116)  评论(0编辑  收藏  举报