码迷,mamicode.com
首页 > 其他好文 > 详细

Luogu P2742 【模板】二维凸包

时间:2019-01-26 11:07:38      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:mes   htm   target   sort   com   scanf   div   www.   ola   

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 【模板】二维凸包

标签:mes   htm   target   sort   com   scanf   div   www.   ola   

原文地址:https://www.cnblogs.com/wifimonster/p/10322629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!