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 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.
/*
poj 2653 线段与线段相交

判断当前线段后面的线段是否与它相交即可

hhh-2016-05-04 22:10:50
*/
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>
#include <functional>
#include <map>
using namespace std;
#define lson  (i<<1)
#define rson  ((i<<1)|1)
typedef long long ll;
const int  maxn = 200000;
double eps = 1e-8;
int tot;
int n,m;
double x1,x2,y1,y2,x3,x4,y3,y4;

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(int _x,int _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;
    }
};

struct Line
{
    Point s,t;
    Line() {}
    Line(Point _s,Point _t)
    {
        s = _s;
        t = _t;
    }
    int operator &(const Line&b)const
    {
        if( sgn((s-t) ^ (b.s-b.t)) == 0)   //通过叉积判断
        {
            return 0;
        }
        return 1;
    }
};

bool inter(Line l1,Line l2)
{
    return
        max(l1.s.x,l1.t.x) >= min(l2.s.x,l2.t.x) &&
        max(l2.s.x,l2.t.x) >= min(l1.s.x,l1.t.x) &&
        max(l1.s.y,l1.t.y) >= min(l2.s.y,l2.t.y) &&
        max(l2.s.y,l2.t.y) >= min(l1.s.y,l1.t.y) &&
        sgn((l2.s-l1.s)^(l1.t-l1.s))*sgn((l2.t-l1.s)^(l1.t-l1.s)) <= 0 &&
        sgn((l1.s-l2.s)^(l2.t-l2.s))*sgn((l1.t-l2.s)^(l2.t-l2.s)) <= 0;
}

int tans[maxn];
Line line[maxn];

int main()
{
    while(scanf("%d",&n) && n)
    {
        memset(tans,1,sizeof(tans));

        for(int i = 0; i < n; i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[i] = Line(Point(x1,y1),Point(x2,y2));
        }
        int num = n;
        for(int i = 0; i < n; i++)
        {
            for(int j = i+1; j < n; j++)
            {
                if(inter(line[i],line[j]))
                {
                    tans[i] = 0;
                    num--;
                    break;
                }
            }
        }
        int cur = 0;
        printf("Top sticks: ");
        for(int i = 0; i < n; i++)
        {
            if(tans[i])
            {
                cur++;
                if(num == cur)
                    printf("%d.\n",i+1);
                else
                    printf("%d, ",i+1);
            }
        }
    }
    return 0;
}

  

时间: 2024-12-29 05:03:42

poj 2653 线段与线段相交的相关文章

POJ - 2653 - Pick-up sticks 线段与线段相交

判断线段与线段相交 莫名其妙的数据量 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cmath> 5 #include <cstring> 6 using namespace std; 7 const double eps = 1e-8; 8 int dcmp(double x) 9 { 10 return fabs(x) < eps ?

POJ 2653 Pick-up sticks [线段相交 迷之暴力]

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12861   Accepted: 4847 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 2653 Pick-up sticks(线段相交)

题意:给定n个木棍依次放下,要求最终判断没被覆盖的木棍是哪些. 思路:快速排斥以及跨立实验可以判断线段相交. 1 #include<algorithm> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<iostream> 6 const double eps=1e-10; 7 struct Point{ 8 double x,y; 9 Point(){} 10

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 finishi

poj 3304 直线与线段相交

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12161   Accepted: 3847 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments

判断线段和直线相交 POJ 3304

1 // 判断线段和直线相交 POJ 3304 2 // 思路: 3 // 如果存在一条直线和所有线段相交,那么平移该直线一定可以经过线段上任意两个点,并且和所有线段相交. 4 5 #include <cstdio> 6 #include <cstring> 7 #include <iostream> 8 #include <algorithm> 9 #include <map> 10 #include <set> 11 #inclu

poj 1269 线段与线段相交

Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13605   Accepted: 6049 Description We all know that a pair of distinct points on a plane defines a line and that a pair of lines on a plane will intersect in one of three

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