zoukankan      html  css  js  c++  java
  • POJ 2653 Pick-up sticks(计算几何)

    Pick-up sticks
    Time Limit: 3000MSMemory Limit: 65536K
    Total Submissions: 7946Accepted: 2916

    Description

    Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to find the top sticks, that is these sticks such that there is no stick on top of them. Stan has noticed that the last thrown stick is always on top but he wants to know all the sticks that are on top. Stan sticks are very, very thin such that their thickness can be neglected.

    Input

    Input consists of a number of cases. The data for each case start with 1 <= n <= 100000, the number of sticks for this case. The following n lines contain four numbers each, these numbers are the planar coordinates of the endpoints of one stick. The sticks are listed in the order in which Stan has thrown them. You may assume that there are no more than 1000 top sticks. The input is ended by the case with n=0. This case should not be processed.

    Output

    For each input case, print one line of output listing the top sticks in the format given in the sample. The top sticks should be listed in order in which they were thrown. 

    The picture to the right below illustrates the first case from input.POJ 2653 Pick-up sticks(计算几何) - qhn999 - 码代码的猿猿

    Sample Input

    5
    1 1 4 2
    2 3 3 1
    1 -2.0 8 4
    1 4 8 2
    3 3 6 -2.0
    3
    0 0 1 1
    1 0 2 1
    2 0 3 1
    0

    Sample Output

    Top sticks: 2, 4, 5.
    Top sticks: 1, 2, 3.

    Hint

    Huge input,scanf is recommended.

    Source


    暴力的时候需要由前面的往后判断,这样就不会达到nlogn了


    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>

    using namespace std;

    const double eps=1e-8;

    int cmp(double x)
    {
        if(fabs(x)<eps) return 0;
        if(x>0) return 1;
        return -1;
    }

    struct point
    {
        double x,y;
        point(double a,double b):x(a),y(b) {}
        point() {}
    };

    struct line
    {
        point a,b;
        line(point x,point y):a(x),b(y)  {}
        line() {}
    };

    double det(const point&a,const point &b)
    {
        return a.x*b.y-a.y*b.x;
    }

    double dot(const point&a,const point &b)
    {
        return a.x*b.x+a.y*b.y;
    }

    point operator-(const point&a,const point&b)
    {
        return point(a.x-b.x,a.y-b.y);
    }

    point operator*(const point&a,const double &b)
    {
        return point(a.x*b,a.y*b);
    }

    point operator*(const double &a,const point &b)
    {
        return point(a*b.x,a*b.y);
    }

    point operator/(const point&a,const double &b)
    {
        return point(a.x/b,a.y/b);
    }

    bool parallel(line a,line b)
    {
        return !cmp(det(a.a-a.b,b.a-b.b));
    }

    bool line_make_point(line a,line b,point &res)
    {
        if(parallel(a,b)) return false;
        double s1=det(a.a-b.a,b.b-b.a);
        double s2=det(a.b-b.a,b.b-b.a);
        res=(s1*a.b-s2*a.a)/(s1-s2);
        return true;
    }

    bool pointonsegment(point p,point s,point t)
    {
        return cmp(det(p-s,t-s))==0&&cmp(dot(p-s,p-t))<=0;
    }
    line LL[100100];int vis[100100];

    int main()
    {
        int n;
    while(scanf("%d",&n)!=EOF&&n)
    {
        memset(vis,-1,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            double a,b,c,d;
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            point A(a,b),B(c,d);
            LL.a=A; LL.b=B;
        }

        for(int i=0;i<n;i++)
        {
            for(int j=i+1;j<n;j++)
            {
                point res;
                res.x=999999;
                res.y=-99999;
                if(vis==0) break;
                if(line_make_point(LL,LL[j],res)&&pointonsegment(res,LL[j].a,LL[j].b)&&pointonsegment(res,LL.b,LL.a))
                {
                    vis=0;
                }
            }
        }

        int first=1;
        printf("Top sticks: ");
        for(int i=0;i<n;i++)
        {
            if(vis==-1)
            {
                if(first==1)
                {
                    first=0;
                }
                else
                {
                    printf(", ");
                }
                printf("%d",i+1);
            }
        }
        printf(". ");
    }
        return 0;
    }

  • 相关阅读:
    java 求 1!+2!+3!+....+10!的和为
    Java 循环控制语句
    java for 循环 九九乘法表
    Java for 循环
    Java while 和 do...while
    Java if语句
    Java switch 语句
    java a++ 和 ++a 理解
    Java 自动转换和强制转换
    二叉树遍历
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350947.html
Copyright © 2011-2022 走看看