• poj 2187 凸包加旋转卡壳算法


    题目链接:http://poj.org/problem?id=2187

    旋转卡壳算法:http://www.cppblog.com/staryjy/archive/2009/11/19/101412.html 或 http://cgm.cs.mcgill.ca/~orm/rotcal.frame.html

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int maxn = 55000;
    const int maxe = 100000;
    const int INF = 0x3f3f3f;
    const double eps = 1e-8;
    const double PI = acos(-1.0);
    
    struct Point{
        double x,y;
        Point(double x=0, double y=0) : x(x),y(y){ }    //构造函数
    };
    typedef Point Vector;
    
    Vector operator + (Vector A , Vector B){return Vector(A.x+B.x,A.y+B.y);}
    Vector operator - (Vector A , Vector B){return Vector(A.x-B.x,A.y-B.y);}
    Vector operator * (Vector A , double p){return Vector(A.x*p,A.y*p);}
    Vector operator / (Vector A , double p){return Vector(A.x/p,A.y/p);}
    
    bool operator < (const Point& a,const Point& b){
        return a.x < b.x ||( a.x == b.x && a.y < b.y);
    }
    
    int dcmp(double x){
        if(fabs(x) < eps) return 0;
        else              return x < 0 ? -1 : 1;
    }
    bool operator == (const Point& a, const Point& b){
        return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
    }
    
    double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y; }
    double Length(Vector A)    { return Dot(A,A); }   //距离的平方;
    double Angle(Vector A, Vector B)  { return acos(Dot(A,B) / Length(A) / Length(B)); }
    double Cross(Vector A, Vector B)  { return A.x*B.y - A.y * B.x; }
    
    //凸包:
    /**Andrew算法思路:首先按照先x后y从小到大排序(这个地方没有采用极角逆序排序,所以要进行两次扫描),删除重复的点后得到的序列p1,p2.....,然后把p1和p2放到凸包中。从p3开始,当新的
    点在凸包“前进”方向的左边时继续,否则依次删除最近加入凸包的点,直到新点在左边;**/
    
    //Goal[]数组模拟栈的使用;
    int ConvexHull(Point* P,int n,Point* Goal){
        sort(P,P+n);
        int m = unique(P,P+n) - P;    //对点进行去重;
        int cnt = 0;
        for(int i=0;i<m;i++){       //求下凸包;
            while(cnt>1 && dcmp(Cross(Goal[cnt-1]-Goal[cnt-2],P[i]-Goal[cnt-2])) <= 0)  cnt--;
            Goal[cnt++] = P[i];
        }
        int temp = cnt;
        for(int i=m-2;i>=0;i--){     //逆序求上凸包;
            while(cnt>temp && dcmp(Cross(Goal[cnt-1]-Goal[cnt-2],P[i]-Goal[cnt-2])) <= 0) cnt--;
            Goal[cnt++] = P[i];
        }
        if(cnt > 1) cnt--;  //减一为了去掉首尾重复的;
        return cnt;
    }
    //旋转卡壳可以用于求凸包的直径、宽度,两个不相交凸包间的最大距离和最小距离
    //计算凸包直径,输入凸包Goal,顶点个数为n,按逆时针排列,输出直径的平方
    double RotatingCalipers(Point* Goal,int n){
        double ret = 0;
        Goal[n]=Goal[0];  //补上使凸包成环;
        int pv = 1;
        for(int i=0;i<n;i++){   //枚举边Goal[i]Goal[i+1],与最远顶点Goal[pv];利用叉积求面积的方法求最大直径;;
            while(fabs(Cross(Goal[i+1]-Goal[pv+1],Goal[i]-Goal[pv+1]))>fabs(Cross(Goal[i+1]-Goal[pv],Goal[i]-Goal[pv])))
                  pv = (pv+1)%n;
            ret=max(ret,max(Length(Goal[i]-Goal[pv]),Length(Goal[i+1]-Goal[pv+1]))); //这个地方不太好理解,就是要考虑当pv与pv+1所在直线平行于i与i+1的情况;
        }
        return ret;
    }
    /*********************************分割线******************************/
    
    Point P[maxn],Goal[maxn];
    int n;
    
    int main()
    {
        //freopen("E:\acm\input.txt","r",stdin);
        cin>>n;
        int cnt = n;
        for(int i=0;i<n;i++){
            double x,y;
            scanf("%lf %lf",&x,&y);
            P[i] = Point(x,y);
        }
        cnt = ConvexHull(P,cnt,Goal);
        double Maxlen = RotatingCalipers(Goal,cnt);
        printf("%.f
    ",Maxlen);
        return 0;
    }
    View Code


    直接枚举凸包的点也可以,n偏小;

    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int maxn = 55000;
    const int maxe = 100000;
    const int INF = 0x3f3f3f;
    const double eps = 1e-8;
    const double PI = acos(-1.0);
    
    struct Point{
        double x,y;
        Point(double x=0, double y=0) : x(x),y(y){ }    //构造函数
    };
    typedef Point Vector;
    
    Vector operator + (Vector A , Vector B){return Vector(A.x+B.x,A.y+B.y);}
    Vector operator - (Vector A , Vector B){return Vector(A.x-B.x,A.y-B.y);}
    Vector operator * (Vector A , double p){return Vector(A.x*p,A.y*p);}
    Vector operator / (Vector A , double p){return Vector(A.x/p,A.y/p);}
    
    bool operator < (const Point& a,const Point& b){
        return a.x < b.x ||( a.x == b.x && a.y < b.y);
    }
    
    int dcmp(double x){
        if(fabs(x) < eps) return 0;
        else              return x < 0 ? -1 : 1;
    }
    bool operator == (const Point& a, const Point& b){
        return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0;
    }
    
    double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y; }
    double Length(Vector A)    { return Dot(A,A); }   //距离的平方;
    double Angle(Vector A, Vector B)  { return acos(Dot(A,B) / Length(A) / Length(B)); }
    double Cross(Vector A, Vector B)  { return A.x*B.y - A.y * B.x; }
    
    //凸包:
    /**Andrew算法思路:首先按照先x后y从小到大排序(这个地方没有采用极角逆序排序,所以要进行两次扫描),删除重复的点后得到的序列p1,p2.....,然后把p1和p2放到凸包中。从p3开始,当新的
    点在凸包“前进”方向的左边时继续,否则依次删除最近加入凸包的点,直到新点在左边;**/
    
    //Goal[]数组模拟栈的使用;
    int ConvexHull(Point* P,int n,Point* Goal){
        sort(P,P+n);
        int m = unique(P,P+n) - P;    //对点进行去重;
        int cnt = 0;
        for(int i=0;i<m;i++){       //求下凸包;
            while(cnt>1 && dcmp(Cross(Goal[cnt-1]-Goal[cnt-2],P[i]-Goal[cnt-2])) <= 0)  cnt--;
            Goal[cnt++] = P[i];
        }
        int temp = cnt;
        for(int i=m-2;i>=0;i--){     //逆序求上凸包;
            while(cnt>temp && dcmp(Cross(Goal[cnt-1]-Goal[cnt-2],P[i]-Goal[cnt-2])) <= 0) cnt--;
            Goal[cnt++] = P[i];
        }
        if(cnt > 1) cnt--;  //减一为了去掉首尾重复的;
        return cnt;
    }
    
    /*********************************分割线******************************/
    
    Point P[maxn],Goal[maxn];
    int n;
    
    int main()
    {
        //freopen("E:\acm\input.txt","r",stdin);
        cin>>n;
        int cnt = n;
        for(int i=0;i<n;i++){
            double x,y;
            scanf("%lf %lf",&x,&y);
            P[i] = Point(x,y);
        }
        cnt = ConvexHull(P,cnt,Goal);
        double Maxlen = 0;
        for(int i=0;i<cnt;i++)
               for(int j=i+1;j<cnt;j++){
                 Maxlen = max(Maxlen,Length(Goal[j]-Goal[i]));
            }
        printf("%.f
    ",Maxlen);
        return 0;
    }
    View Code
  • 相关阅读:
    restframework 自定义返回响应格式
    restframework 分页器
    Python设计模式
    Pytest系列
    Pytest系列
    Pytest系列 -pytest-dependency 用例依赖
    restframework jwt登录验证
    restframework 自定义json返回格式
    Axure RP8 注册码
    LVM 移除PV步骤
  • 原文地址:https://www.cnblogs.com/acmdeweilai/p/3254015.html
Copyright © 2020-2023  润新知