• uva1453 旋转卡壳算法


    链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4199

    题意:给出若干个正方形,求出他们的顶点中距离最大的两个点间的距离的平方。

    思路:很直接的求点集的直径,采用旋转卡壳算法。算法参考:http://www.cppblog.com/staryjy/archive/2010/09/25/101412.html

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int maxn=1000000;
    const double eps=1e-8;
    struct Point
    {
        int x,y;
        Point(int x=0,int y=0):x(x),y(y) {}
    }p[maxn],ch[maxn];
    
    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);
    }
    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 Cross(Vector A,Vector B)
    {
        return A.x*B.y-A.y*B.x;
    }
    int dist2(Point a,Point b)
    {
        return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    }
    int cmp(const Point &a,const Point &b)
    {
        if(a.x<b.x) return 1;
        else if(a.x==b.x)
        {
            if(a.y<b.y) return 1;
            else return 0;
        }
        else return 0;
    }
    int ConvexHull(Point *p,int n,Point *ch)
    {
        sort(p,p+n,cmp);
        n=unique(p,p+n)-p;//去重
        int m=0;
        for(int i=0;i<n;i++)
        {
            while(m>1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]) <= 0)
                m--;
            ch[m++]=p[i];
        }
        int k=m;
        for(int i=n-2;i>=0;i--)
        {
            while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2]) <= 0)
                m--;
            ch[m++]=p[i];
        }
        if(n>1) m--;
        return m;
    }
    
    int RotatingCalipers(Point *ch,int n)//计算凸包直径,凸包顶点数组ch,顶点个数n,按逆时针排列,输出直径的平方
    {
        int q=1;
        int ans=0;
        ch[n]=ch[0];
        for(int p=0;p<n;p++)
        {
            while(Cross(ch[p+1]-ch[p],ch[q+1]-ch[p])>Cross(ch[p+1]-ch[p],ch[q]-ch[p]))
                q=(q+1)%n;
            ans=max(ans,max(dist2(ch[p],ch[q]),dist2(ch[p+1],ch[q+1])));
        }
        return ans;
    }
    int main()
    {
        //freopen("ine.cpp","r",stdin);
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n,k=0;
            scanf("%d",&n);
            while(n--)
            {
                int x,y,w;
                scanf("%d%d%d",&x,&y,&w);
                p[k++]=Point(x,y);
                p[k++]=Point(x+w,y);
                p[k++]=Point(x,y+w);
                p[k++]=Point(x+w,y+w);
            }
            int m=ConvexHull(p,k,ch);
            int ans=RotatingCalipers(ch,m);
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    模板网站
    用servlet和jsp做探索数据库
    Hibernate和jsp做数据库单表的增删改查
    拦截器
    校验器-对提交的用户名和密码进行过滤
    使用my exclipse对数据库进行操作(4)
    如何正确关闭游戏服务器
    Ehcache 入门详解
    自动重置 Language Level默认为5与 Java Complier默认为1.5
    洪均生谈初学者练习(怎样认识太级拳和怎样进行练习)
  • 原文地址:https://www.cnblogs.com/54zyq/p/3234714.html
Copyright © 2020-2023  润新知