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

    Pick-up sticks
    Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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.
    

    Hint

    Huge input,scanf is recommended.


    枚举每条线段,如果它上面没有和它相交的

    感觉下面应该会超时的,但是数据水,还没超。。。

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    #include <map>
    #include <vector>
    #include <set>
    #include <string>
    #include <math.h>
    
    using namespace std;
    const double eps = 1e-8;
    int sgn(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);
        }
        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;
        }
    };
    struct Line
    {
        Point s,e;
        Line(){}
        Line(Point _s,Point _e)
        {
            s = _s;e = _e;
        }
    };
    //判断线段相交
    bool inter(Line l1,Line l2)
    {
        return ///感觉前面几个max>min的比较都是没什么用的,直接叉乘判断就行了,并且没有这四个比较提交也能过
            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) &&
            sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s)) <= 0 &&
            sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= 0;
    }
    const int MAXN = 100010;
    Line line[MAXN];
    bool flag[MAXN];
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        //freopen("out.txt","w",stdout);
        int n;
        double x1,y1,x2,y2;
        while(scanf("%d",&n)==1 && n)
        {
            for(int i = 1;i <= n;i++)
            {
                scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
                line[i] = Line(Point(x1,y1),Point(x2,y2));
                flag[i] = true;
            }
            for(int i = 1;i <= n;i++)
            {
                for(int j = i+1;j <= n;j++)
                    if(inter(line[i],line[j]))
                    {
                        flag[i] = false;
                        break;
                    }
            }
            printf("Top sticks: ");
            bool first = true;
            for(int i = 1;i <= n;i++)
                if(flag[i])
                {
                    if(first)first = false;
                    else printf(", ");
                    printf("%d",i);
                }
            printf(".
    ");
        }
    
        return 0;
    }
    

    一种队列实现的:不过时间好像更长了。。。

    /*
    Memory 196K
    Time  563MS
    */
    #include <iostream>
    #include <queue>
    #include <cstdio>
    using namespace std;
    #define min(a,b) (a>b?b:a)
    #define max(a,b) (a<b?b:a)
    
    typedef struct{
    	int cnt;
    	double x1,y1,x2,y2;
    }Line;
    
    int n;
    
    double direction(double x,double y,double x1,double y1,double x2,double y2){	//叉积
    	double a1=x1-x;
    	double b1=y1-y;
    	double a2=x2-x;
    	double b2=y2-y;
    	return a1*b2-a2*b1;
    }
    
    int on_segment(double x1,double y1,double x2,double y2,double x,double y){		//判断共线
    	if((min(x1,x2)<=x && x<=max(x1,x2)) && (min(y1,y2)<=y && y<=max(y1,y2)))
    		return 1;
    	return 0;
    }
    
    int cross(Line v,Line t){		//是否交叉,根据算法导论写的
    	double d1,d2,d3,d4;
    	d1=direction(t.x1,t.y1,t.x2,t.y2,v.x1,v.y1);	//算叉积
    	d2=direction(t.x1,t.y1,t.x2,t.y2,v.x2,v.y2);
    	d3=direction(v.x1,v.y1,v.x2,v.y2,t.x1,t.y1);
    	d4=direction(v.x1,v.y1,v.x2,v.y2,t.x2,t.y2);
    	if(d1*d2<0 && d3*d4<0) return 1;		//直接和0比较的话时间是625MS
    	if(!d1 && on_segment(t.x1,t.y1,t.x2,t.y2,v.x1,v.y1)) return 1;
    	if(!d2 && on_segment(t.x1,t.y1,t.x2,t.y2,v.x2,v.y2)) return 1;
    	if(!d3 && on_segment(v.x1,v.y1,v.x2,v.y2,t.x1,t.y1)) return 1;
    	if(!d4 && on_segment(v.x1,v.y1,v.x2,v.y2,t.x2,t.y2)) return 1;
    	return 0;
    }
    
    void input(){
    	queue <Line>q;
    	Line v,t;
    	int i;
    	scanf("%lf%lf%lf%lf",&v.x1,&v.y1,&v.x2,&v.y2);
    	v.cnt=1;
    	q.push(v);
    
    	for(i=2;i<=n;i++){
    		scanf("%lf%lf%lf%lf",&t.x1,&t.y1,&t.x2,&t.y2);
    		t.cnt=i;
    		q.push(t);		//用当前的作为队尾
    		while(!q.empty()){
    			v=q.front();q.pop();		//一个一个出队,直到队尾
    			if(t.cnt==v.cnt) {
    				q.push(t);
    				break;
    			}
    			if(!cross(v,t)) q.push(v);		//如果不交叉继续入队
    		}
    	}
    
    	v=q.front();q.pop();
    	printf("Top sticks: %d",v.cnt);
    	while(!q.empty()){
    		v=q.front();q.pop();
    		printf(", %d",v.cnt);
    	}
    	printf(".
    ");
    }
    
    int main(){
    	while(scanf("%d",&n) && n){
    		input();
    	}
    	return 0;
    }
    





  • 相关阅读:
    Mysql中limit的用法详解
    EXCEPTION与ERROR的区别
    调用sed命令的三种方式
    学编程一定要上大学?美国一半码农都没有计算机学位
    awk 循环语句例子
    Ubuntu 16.04 LTS今日发布
    Ubuntu启动eclipse问题
    Vim技能修炼教程(6)
    Vim技能修炼教程(7)
    Vim技能修炼教程(5)
  • 原文地址:https://www.cnblogs.com/zswbky/p/6717937.html
Copyright © 2011-2022 走看看