zoukankan      html  css  js  c++  java
  • POJ 2653 Pick-up sticks (线段相交)

    题目:

    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.

    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.

    题意:给出n条线段 按照先后顺序 如果后来有线段和其相交 就当作覆盖掉这条线段 求没有被覆盖的线段
    思路:数据只有1000 可以暴力相求

    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int inf=0x3f3f3f3f;
    const int maxn=1e5+10;
    const double eps=1e-10;
    int n;
    double x,y,xx,yy;
    int vis[maxn];
    
    int dcmp(double x){
        if(fabs(x)<eps) return 0;
        if(x<0) return -1;
        else return 1;
    }
    
    struct Point{
        double x,y;
        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.x+y*b.y;
        }
        double operator ^ (const Point &b) const{
            return x*b.y-y*b.x;
        }
    };
    
    struct Line{
        Point s,e;
        Line(){}
        Line(Point _s,Point _e){
            s=_s,e=_e;
        }
    }line[maxn];
    
    bool inter(Line l1,Line l2){
        return 
            max(l1.s.x,l1.e.x)>=min(l2.s.x,l2.e.x) &&
            max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x) &&
            max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y) &&
            max(l2.s.y,l2.e.y)>=min(l1.s.y,l1.e.y) &&
            dcmp((l2.s-l1.s)^(l1.e-l1.s))*dcmp((l2.e-l1.s)^(l1.e-l1.s))<=0 &&
            dcmp((l1.s-l2.s)^(l2.e-l2.s))*dcmp((l1.e-l2.s)^(l2.e-l2.s))<=0;
    
    }
    
    
    int main(){
        while(~scanf("%d",&n)){
            if(n==0) break;
            for(int i=1;i<=n;i++){
                scanf("%lf%lf%lf%lf",&x,&y,&xx,&yy);
                line[i]=Line(Point(x,y),Point(xx,yy));
                vis[i]=1;
            } 
            for(int i=1;i<=n;i++){
                for(int j=i+1;j<=n;j++){
                    if(inter(line[i],line[j])){
                        vis[i]=0;
                        break;
                    }
                }
            }
            printf("Top sticks: ");
            int flag=1;
            for(int i=1;i<=n;i++){
                if(vis[i]){
                    if(flag) flag=0;
                    else printf(", ");
                    printf("%d",i);
                }
            }
            printf(".
    ");
        }
        return 0;
    }
    
    
    
     
  • 相关阅读:
    关于自动分裂的思考 | Solrex 杨文博的博客,记录我的生活、技术、思想和梦想
    在STL中,map按值来排序的实现方法_永不言弃是生命的基调!_百度空间
    C/C++学习路线(教材推荐)_Hello World!_百度空间
    Google C++ Style中允许使用的Boost库(1) 程序即人生 博客频道 CSDN.NET
    STL中map按值(value)排序
    程序即人生 » 移动平台现在可用的C++ 11特性
    开发者应该了解的 12 款 Eclipse 插件 编程语言 ITeye资讯
    Lisp的给力特性(V.S. Python3) 第二篇 程序即人生 博客频道 CSDN.NET
    Solidot | 地球上有多少Java程序员?
    在STL中,map按值来排序的实现方法
  • 原文地址:https://www.cnblogs.com/whdsunny/p/9842839.html
Copyright © 2011-2022 走看看