poj 2653 (线段相交判断)

http://poj.org/problem?id=2653

Pick-up sticks

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 9531   Accepted: 3517

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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <algorithm>

using namespace std;
#define MAXX 100010
#define eps 1e-6

typedef struct point
{
    double x,y;
}point;

typedef struct
{
    point st,ed;
}line;

bool  xy(double x,double y){ return x<y-eps; }
bool  dy(double x,double y){ return x>y+eps; }
bool xyd(double x,double y){ return x<y+eps; }
bool dyd(double x,double y){ return x>y-eps; }
bool  dd(double x,double y){ return fabs(x-y)<eps; }

double crossProduct(point a,point b,point c)
{
    return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}

bool onSegment(point a,point b,point c)
{
    double maxx=max(a.x,b.x);
    double maxy=max(a.y,b.y);
    double minx=min(a.x,b.x);
    double miny=min(a.y,b.y);
    if(dd(crossProduct(a,b,c),0.0)&&dyd(c.x,minx)
           &&xyd(c.x,maxx)&&dyd(c.y,miny)&&xyd(c.y,maxy))
        return true;
    return false;
}

bool segIntersect(point p1,point p2,point p3,point p4)
{
    double d1=crossProduct(p3,p4,p1);
    double d2=crossProduct(p3,p4,p2);
    double d3=crossProduct(p1,p2,p3);
    double d4=crossProduct(p1,p2,p4);
    if(xy(d1*d2,0.0)&&xy(d3*d4,0.0))
        return true;
    if(dd(d1,0.0)&&onSegment(p3,p4,p1))
        return true;
    if(dd(d2,0.0)&&onSegment(p3,p4,p2))
        return true;
    if(dd(d3,0.0)&&onSegment(p1,p2,p3))
        return true;
    if(dd(d4,0.0)&&onSegment(p1,p2,p4))
        return true;
    return false;
}

point p[MAXX];
line li[MAXX];
int num[MAXX];

int main()
{
    int m,n,i,j;
    while(scanf("%d",&n)!=EOF&&n)
    {
        memset(num,0,sizeof(num));
        for(i=0;i<n;i++)
        {
            scanf("%lf%lf%lf%lf",&li[i].st.x,&li[i].st.y,&li[i].ed.x,&li[i].ed.y);
        }
        for(i=0;i<n;i++)
        {
            for(j=i+1;j<n;j++)
            {
                if(segIntersect(li[i].st,li[i].ed,li[j].st,li[j].ed))
                {
                    num[i]++;
                    break;
                }

            }
        }
        int cas=0,tes=0,tmp;
        //int str[MAXX];
        for(i=0;i<n;i++)
        {
            if(num[i] == 0)
            {
                cas++;
            }
        }
        printf("Top sticks: ");
        for(i=0;i<n;i++)
        {
            if(num[i] == 0 && tes<cas-1)
            {
                printf("%d, ",i+1);
                tes++;
                tmp=i;
            }
        }//printf("%d**",i);
        for(j=tmp+1;j<n;j++)
        {
            if(num[j] == 0)
            {
                printf("%d.\n",j+1);
            }
        }
    }
    return 0;
}

poj 2653 (线段相交判断),布布扣,bubuko.com

时间: 2024-10-13 02:23:06

poj 2653 (线段相交判断)的相关文章

poj 1410 线段相交判断

http://poj.org/problem?id=1410 Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11329   Accepted: 2978 Description You are to write a program that has to decide whether a given line segment intersects a given rectangle. An ex

poj 2653 线段相交

题意:一堆线段依次放在桌子上,上面的线段会压住下面的线段,求找出没被压住的线段. sol:从下向上找,如果发现上面的线段与下面的相交,说明被压住了.break掉 其实这是个n^2的算法,但是题目已经说了没被压住的线段不超过1000个,所以不会爆 1 #include<math.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 bool ans[100100]; 6 int n; 7 double X1,X2,Y1,Y2;

Pick-up sticks - POJ 2653 (线段相交)

题目大意:有一个木棒,按照顺序摆放,求出去上面没有被别的木棍压着的木棍..... 分析:可以维护一个队列,如果木棍没有被压着就入队列,如果判断被压着,就让那个压着的出队列,最后把这个木棍放进队列,不过速度并不快,枚举才是最快的......据说是任意时刻没有超过1000个top sticks.....很难注意到. 代码如下: =======================================================================================

poj 2653 线段与线段相交

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11884   Accepted: 4499 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 fin

POJ 1410 Intersection(线段相交&amp;&amp;判断点在矩形内&amp;&amp;坑爹)

Intersection 大意:给你一条线段,给你一个矩形,问是否相交. 相交:线段完全在矩形内部算相交:线段与矩形任意一条边不规范相交算相交. 思路:知道具体的相交规则之后题其实是不难的,但是还有个坑点就是题目里明明说给的是矩形左上角跟右下角的点,但实际上不是,需要重新判断一下...真坑. 1 struct Point 2 { 3 double x, y; 4 } A, B, C, D; 5 struct Line 6 { 7 Point a, b; 8 } L; 9 10 int n; 11

POJ 2653 线段交

思路: 运用队列存储没有被覆盖的木棍,没加入一个棍子,就要判断一下是否队列中的棍子被覆盖,如果被覆盖,就从队列中删除: 线段交判断方法:跨立实验 Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9698 Accepted: 3591 Description Stan has n sticks of various length. He throws them one at a time on th

zoj 1010 (线段相交判断+多边形求面积)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 Area Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Jerry, a middle school student, addicts himself to mathematical research. Maybe the problems he has thought are

ACM1558两线段相交判断和并查集

Segment set Problem Description A segment and all segments which are connected with it compose a segment set. The size of a segment set is the number of segments in it. The problem is to find the size of some segment set. Input In the first line ther

poj 1066 线段相交

链接:http://poj.org/problem?id=1066 Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5431   Accepted: 2246 Description Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid