zoukankan      html  css  js  c++  java
  • HDU 4946 共线凸包

    题目大意:

      一些点在一张无穷图上面,每个点可以控制一些区域,这个区域满足这个点到达这个区域的时间严格小于其他点。求哪些点能够控制无穷面积的区域。

    题目思路:

      速度小的控制范围一定有限。

      速度最大当且仅当在凸包上才能够控制无穷区域。可以通过,任意两个点中垂线为界,左右各控制一半,判断出凸包内的点仅能控制有限区域。

       

      特判:

        速度最大且在同一个点上的点均不能控制无穷区域,但是要加入凸包计算。

        速度最大为0不能控制无穷区域。

    对于共线凸包(Graham),(代码中有解释)

      均不能存在重点!可用map判重。

       1、按极角坐标序排

          缺点:需要将最后一条边上的点逆序排,才能够将最后一边共线点加入凸包。

       2、按水平序排。 

          缺点:若所有点在一条直线上,会产生将所有点入凸包1~n~2的情况,需要特判,当然本题只是用到这些点,无需判断是否重复出现。

     极角序:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <utility>
    #include <stack>
    #include <queue>
    #include <map>
    #include <deque>
    #define max(x,y) ((x)>(y)?(x):(y))
    #define min(x,y) ((x)<(y)?(x):(y))
    #define INF 0x3f3f3f3f
    
    using namespace std;
    
    const int MAXN = 1010;
    const double eps = 1e-8;
    const double PI = acos(-1.0);
    
    int tx,ty,tv,maxv,n,N,cas;
    bool pd[MAXN];
    
    int sgn(double x)
    {
        if(fabs(x) < eps) return 0;
        if(x < 0) return -1;
        return 1;
    }
    struct Point
    {
        double x,y;
        int re;
        Point(){}
        Point(double _x, double _y): x(_x),y(_y) {}
        Point operator -(const Point &B) const
        {
            return Point(x-B.x, y-B.y);
        }
        Point operator +(const Point &B) const //向量相加
        {
            return Point(x+B.x, y+B.y);
        }
        double operator ^(const Point &B) const //叉积
        {
            return x*B.y - y*B.x;
        }
        double operator *(const Point &B) const //点积
        {
            return x*B.x + y*B.y;
        }
        bool operator ==(const Point &B) const
        {
            return fabs(B.x-x)<eps && fabs(B.y-y)<eps;
        }
        bool operator !=(const Point &B) const
        {
            return !((*this) == B);
        }
        double norm()//向量的模
        {
            return sqrt(x*x+y*y);
        }
        void transXY(double B) //绕原点逆时针旋转B弧度
        {
            double tx = x, ty = y;
            x = tx*cos(B) - ty*sin(B);
            y = tx*sin(B) + ty*cos(B);
        }
        void input() //读入只能用double读入
        {
            scanf("%lf%lf",&x,&y);
        }
    };
    
    struct Line
    {
        Point s,e;
        Line(){}
        Line(Point _s, Point _e)
        {
            s=_s; e=_e;
        }
    };
    
    double dist(Point a, Point b)
    {
        return sqrt((a-b)*(a-b));
    }
    
    //判断点在线段上
    bool OnS(Point A, Line a)
    {
        return
            sgn((a.s-A)^(a.e-A)) == 0 &&
            sgn((A.x-a.s.x)*(A.x-a.e.x)) <= 0 &&
            sgn((A.y-a.s.y)*(A.y-a.e.y)) <= 0;
    }
    
    //求凸包 Graham算法
    //点的编号0~n-1
    //返回凸包结果Stack[0~top-1]为凸包的编号
    //一个点或两个点 则凸包为一或二个点
    int Stack[MAXN],top;
    Point vertex[MAXN];
    bool Graham_cmp(Point A, Point B)
    {
        double tmp=(A-vertex[0])^(B-vertex[0]);
        if(sgn(tmp) > 0) return 1;
        if(sgn(tmp) == 0 && sgn(dist(A,vertex[0])-dist(B,vertex[0])) <= 0) return 1;
        return 0;
    }
    void Graham(int n)
    {
        int k=0;
        for(int i=1; i<n; i++)
            if((vertex[k].y>vertex[i].y) || (vertex[k].y==vertex[i].y && vertex[k].x>vertex[i].x))
                k=i;
        swap(vertex[0], vertex[k]);
        sort(vertex+1, vertex+n, Graham_cmp);
        if(n == 1)
        {
            top=1;
            Stack[0]=0;
            return;
        }
        if(n == 2)
        {
            top=2;
            Stack[0]=0;
            Stack[1]=1;
            return;
        }
    
        int tmp;
        for(tmp=n-1; tmp>1 && sgn((vertex[0]-vertex[tmp])^(vertex[0]-vertex[tmp-1])) == 0; tmp--);
        reverse(vertex+tmp,vertex+n);//最后一条边倒序
    
        Stack[0]=0;
        Stack[1]=1;
        top=2;
        for(int i=2; i<n; i++)
        {
            while(top > 1 && (vertex[i] == vertex[Stack[top-1]] || sgn((vertex[Stack[top-1]]-vertex[Stack[top-2]])^(vertex[i]-vertex[Stack[top-2]])) < 0))//相同点只进栈一次 同一条线上的点也进栈
                top--;
            Stack[top++]=i;
        }
    }
    
    int main()
    {
        // freopen("1002.in","r",stdin);
        // freopen("1002p.out","w",stdout);
        while(scanf("%d",&N)!=EOF && N)
        {
               memset(pd,0,sizeof(pd));
            n=0;
            maxv=-1;
            for(int i=0; i<N; i++)
            {
                scanf("%d%d%d",&tx,&ty,&tv);
                if(maxv==tv)
                {
                    vertex[n].x=tx;
                    vertex[n].y=ty;
                    vertex[n].re=i;
                    n++;
                }
                else
                if(maxv<tv)
                {
                    maxv=tv;
                    n=0;
                    vertex[n].x=tx;
                    vertex[n].y=ty;
                    vertex[n].re=i;
                    n++;
                }
            }
               Graham(n);
    
               for(int i=0; i<top; i++)
                   pd[vertex[Stack[i]].re]=1;
    
               for(int i=0; i<n; i++)//去掉相同点
                   for(int j=i+1; j<n; j++)
                   if(vertex[i]==vertex[j])
                   {
                       pd[vertex[i].re]=0;
                       pd[vertex[j].re]=0;
                   }
    
               printf("Case #%d: ",++cas);
               for(int i=0; i<N; i++)
               {
                   if(maxv==0) printf("0");
                       else printf("%d",pd[i]);
               }
               printf("
    ");
        }
        return 0;
    }
    
    /*
    0 0 1
    0 1
    0 1
    1 1
    2 1
    3 1
    1 1
    
    */
    View Code

     水平序:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    #include <utility>
    #include <stack>
    #include <queue>
    #include <map>
    #include <deque>
    #define max(x,y) ((x)>(y)?(x):(y))
    #define min(x,y) ((x)<(y)?(x):(y))
    #define INF 0x3f3f3f3f
    
    using namespace std;
    
    const int MAXN = 1010;
    const double eps = 1e-8;
    const double PI = acos(-1.0);
    
    int tx,ty,tv,maxv,n,N,cas;
    int pd[MAXN];
    
    int sgn(double x)
    {
        if(fabs(x) < eps) return 0;
        if(x < 0) return -1;
        return 1;
    }
    struct Point
    {
        double x,y;
        int re;
        Point(){}
        Point(double _x, double _y): x(_x),y(_y) {}
        Point operator -(const Point &B) const
        {
            return Point(x-B.x, y-B.y);
        }
        Point operator +(const Point &B) const //向量相加
        {
            return Point(x+B.x, y+B.y);
        }
        double operator ^(const Point &B) const //叉积
        {
            return x*B.y - y*B.x;
        }
        double operator *(const Point &B) const //点积
        {
            return x*B.x + y*B.y;
        }
        bool operator ==(const Point &B) const
        {
            return fabs(B.x-x)<eps && fabs(B.y-y)<eps;
        }
        bool operator !=(const Point &B) const
        {
            return !((*this) == B);
        }
        double norm()//向量的模
        {
            return sqrt(x*x+y*y);
        }
        void transXY(double B) //绕原点逆时针旋转B弧度
        {
            double tx = x, ty = y;
            x = tx*cos(B) - ty*sin(B);
            y = tx*sin(B) + ty*cos(B);
        }
        bool operator<(const Point B) const
        {
            return(x<B.x || (x==B.x && y<B.y));
        }
        void input() //读入只能用double读入
        {
            scanf("%lf%lf",&x,&y);
        }
    };
    
    struct Line
    {
        Point s,e;
        Line(){}
        Line(Point _s, Point _e)
        {
            s=_s; e=_e;
        }
    };
    
    double dist(Point a, Point b)
    {
        return sqrt((a-b)*(a-b));
    }
    
    //判断点在线段上
    bool OnS(Point A, Line a)
    {
        return
            sgn((a.s-A)^(a.e-A)) == 0 &&
            sgn((A.x-a.s.x)*(A.x-a.e.x)) <= 0 &&
            sgn((A.y-a.s.y)*(A.y-a.e.y)) <= 0;
    }
    
    //求凸包 Graham算法
    //点的编号0~n-1
    //返回凸包结果Stack[0~top-1]为凸包的编号
    //一个点或两个点 则凸包为一或二个点
    int Stack[MAXN],top;
    Point vertex[MAXN];
    bool Graham_cmp(Point A, Point B)
    {
        return A.y<B.y || (A.y == B.y && A.x<B.x);
    }
    void Graham(int n)
    {
        sort(vertex, vertex+n, Graham_cmp);
        top=0;
        for(int i=0; i<n; i++)
        {
            while(top > 1 && sgn((vertex[Stack[top-1]]-vertex[Stack[top-2]])^(vertex[i]-vertex[Stack[top-2]])) < 0)//改为<即可
                top--;
            Stack[top++]=i;
        }
        int tmp=top;
        for(int i=n-2; i>=0; i--)
        {
            while(top > tmp && sgn((vertex[Stack[top-1]]-vertex[Stack[top-2]])^(vertex[i]-vertex[Stack[top-2]])) < 0)
                top--;
            Stack[top++]=i;
        }
        if(n>1) top--;
    }
    
    int main()
    {
        freopen("1002.in","r",stdin);
        freopen("1002p.out","w",stdout);
        map<Point,int> mapp;
        while(scanf("%d",&N)!=EOF && N)
        {
               memset(pd,-1,sizeof(pd));
            n=0;
            maxv=-1;
            for(int i=1; i<=N; i++)
            {
                scanf("%d%d%d",&tx,&ty,&tv);
                if(maxv==tv && mapp[Point(tx,ty)]>0)
                {
                    pd[mapp[Point(tx,ty)]]=0;
                    pd[i]=0;
                    continue;
                }
                if(maxv==tv)
                {
                    vertex[n].x=tx;
                    vertex[n].y=ty;
                    vertex[n].re=i;
                       mapp[vertex[n]]=i;
                    n++;
                }
                if(maxv<tv)
                {
                    mapp.clear();
                    maxv=tv;
                    n=0;
                    vertex[n].x=tx;
                    vertex[n].y=ty;
                    vertex[n].re=i;
                    mapp[vertex[n]]=i;
                    n++;
                }
            }
    
               Graham(n);
    
               for(int i=0; i<top; i++)
               if(pd[vertex[Stack[i]].re]==-1)
                   pd[vertex[Stack[i]].re]=1;
    
               printf("Case #%d: ",++cas);
               for(int i=1; i<=N; i++)
                   if(maxv==0) printf("0");
                   else
                   { 
                       if(pd[i]<=0) printf("0");
                       else printf("1");
                   }
               printf("
    ");
        }
        return 0;
    }
    
    /*
    0 0 1
    0 1
    0 0
    0 1
    0 1
    0 0 1
    0 1
    0 1
    1 1
    2 1
    3 1
    1 1
    
    */
    View Code

     水平序优化:(可以解决重点+共线凸包问题)

      vis判水平序的点是否访问过,防止一条线的情况。

      pd判是否重点,在水平排序后相邻的一定相邻!写的还算漂亮。毕竟map太暴力了。

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <cstdlib>
    #include <cmath>
    #include <utility>
    #include <vector>
    #include <queue>
    #include <map>
    #include <set>
    #define max(x,y) ((x)>(y)?(x):(y))
    #define min(x,y) ((x)>(y)?(y):(x))
    #define MAXN 505
    #define eps 1e-4
    
    using namespace std;
    
    struct Point{
        double x,y;
        int res;
        Point(){}
        Point(double _x, double _y): x(_x),y(_y){}
        double operator^(Point A)
        {
            return x*A.y-A.x*y;
        }
        Point operator -(const Point A) const
        {
            return Point(x-A.x,y-A.y);
        }
    }vertex[MAXN];
    
    int Stack[MAXN],top;
    int N,n,x,y,v,Case;
    bool vis[MAXN],pd[MAXN];
    
    inline double dist(Point A)
    {
        return sqrt(A.x*A.x+A.y*A.y);
    }
    
    int sgn(double x)
    {
        if(fabs(x)<eps) return 0;
        if(x<0) return -1;
        return 1;
    }
    
    bool cmp(Point A, Point B)
    {
        return A.y<B.y || (A.y==B.y && A.x<B.x);
    }
    
    void Graham(int n)
    {
        sort(vertex,vertex+n,cmp);
        for(int i=0; i<n-1; i++)
            if(sgn(dist(vertex[i]-vertex[i+1]))==0)
                pd[vertex[i].res]=pd[vertex[i+1].res]=0;
        top=0;
        for(int i=0; i<n; i++)
        {
            while(top>1 && (sgn(dist(vertex[Stack[top-1]]-vertex[Stack[top-2]]))==0 || sgn((vertex[Stack[top-1]]-vertex[Stack[top-2]])^(vertex[i]-vertex[Stack[top-1]]))<0))
                vis[Stack[--top]]=0;
            Stack[top++]=i;
            vis[i]=1;
        }
        int tmp=top;
        for(int i=n-2; i>=0; i--)
        {
            while(top>tmp && (sgn(dist(vertex[Stack[top-1]]-vertex[Stack[top-2]]))==0 || sgn((vertex[Stack[top-1]]-vertex[Stack[top-2]])^(vertex[i]-vertex[Stack[top-1]]))<0))
                vis[Stack[--top]]=0;
            if(!vis[i]) Stack[top++]=i;
        }
        //if(n>1) top--;
    }
    
    int main()
    {
        while(scanf("%d",&N)!=EOF && N)
        {
            int maxv=-1;
            for(int i=0; i<N; i++)
            {
                scanf("%d%d%d",&x,&y,&v);
                if(v<maxv) continue;
                if(v>maxv) 
                {
                    maxv=v;
                    n=0;
                }
                vertex[n].x=x;
                vertex[n].y=y;
                vertex[n].res=i;
                n++;
            }
            memset(vis,0,sizeof(vis));
            memset(pd,1,sizeof(pd));
            Graham(n);
            // for(int i=0; i<top; i++)
            //     printf("%f %f
    ",vertex[Stack[i]].x,vertex[Stack[i]].y);
            printf("Case #%d: ",++Case);
            memset(vis,0,sizeof(vis));
            if(maxv>0)
            {
                for(int i=0; i<top; i++)
                    vis[vertex[Stack[i]].res]=1;
            }
            for(int i=0; i<N; i++)
                printf("%d",vis[i]&&pd[i]);
            printf("
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    day23-json、pickle、configparser、hashlib、suprocess模块
    day22-时间模块、random模块、os模块、sys模块
    day21-py文件作用,导包、模块搜索路径
    day20-python-二分、面向过程思想、函数式、模块
    day19-python-叠加装饰器分析、yield、三元、生成式、递归
    day18-python-装饰器、迭代器、生成器
    day17-python-装饰器
    day16-python-函数对象、函数嵌套、闭包函数
    搭建yum本地源_阿里云CentOS服务器初始化设置
    ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql 启动不了
  • 原文地址:https://www.cnblogs.com/Mathics/p/3917693.html
Copyright © 2011-2022 走看看