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

Pick-up sticks

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 10330   Accepted: 3833

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.

直接暴力枚举每一条线段就行了,不过有个顺序问题,假设枚举第i个,如果往前枚举的话,排除前面的,这样会超时,如果判断第i个,从第i个往后枚举,不会超时

/*************************************************************************
    > File Name: poj_2653.cpp
    > Author:
    > Mail:
    > Created Time: 2015年04月02日 星期四 21时27分10秒
 ************************************************************************/

#include<iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 100006;
bool vis[N];
struct point{
    double x, y;
};
struct Line{
    point Start, End;
};
Line line[N];
int n;
double Min(double a, double b)
{
    return a < b ? a : b;
}
double Max(double a, double b)
{
    return a > b ? a : b;
}
double get_direction(point a, point b, point c)
{
    point t1, t2;
    t1.x = c.x - a.x; t1.y = c.y - a.y;
    t2.x = b.x - a.x; t2.y = b.y - a.y;
    return (t1.x * t2.y - t2.x * t1.y);
}
bool on_segment(point a, point b, point c)
{
    double minx = Min(a.x, b.x);
    double maxx = Max(a.x, b.x);
    double miny = Min(a.y, b.y);
    double maxy = Max(a.y, b.y);
    return (c.x <= maxx && c.x >= minx && c.y <= maxy && c.y >= miny);
}
bool segment_intersect(point a, point b, point c, point d)//判断线段是否相交
{
    double d1 = get_direction(a, b, c);
    double d2 = get_direction(a, b, d);
    double d3 = get_direction(c, d, a);
    double d4 = get_direction(c, d, b);
    if (d1 * d2 < 0 && d3 * d4 < 0)
        return true;//规范相交
    //下面的四个都是非规范相交
    if (d1 == 0 && on_segment(a, b, c))
        return true;
    if (d2 == 0 && on_segment(a, b, d))
        return true;
    if (d3 == 0 && on_segment(c, d, a))
        return true;
    if (d4 == 0 && on_segment(c, d, b))
        return true;
    return false;
}
void check_segments(int m)
{
    for (int i = m + 1; i < n; i++)
    {
        if (segment_intersect(line[i].Start, line[i].End, line[m].Start, line[m].End))
        {
            vis[m] = true;
            return;
        }
    }
}
int main()
{
    while (~scanf("%d", &n) && n)
    {
        memset(vis, false, sizeof(vis));
        for (int i = 0; i < n; i++)
        {
            scanf("%lf %lf %lf %lf", &line[i].Start.x, &line[i].Start.y, &line[i].End.x, &line[i].End.y);
        }
        for (int i = 0; i < n; i++)
            check_segments(i);
        printf("Top sticks: ");
        bool first = true;
        for (int i = 0; i < n; i++)
        {
            if (!vis[i])
            {
                if (first)
                    first = false;
                else
                    printf(", ");
                printf("%d", i + 1);
            }
        }
        printf(".\n");
    }

    return 0;
}

时间: 2024-10-12 22:21:28

POJ 2653 Pick-up sticks (判断线段相交)的相关文章

【POJ 2653】Pick-up sticks 判断线段相交

一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define N 100005 using namespace std; struct Point { double x

POJ2653 Pick-up sticks 判断线段相交

POJ2653 判断线段相交的方法 先判断直线是否相交 再判断点是否在线段上 复杂度是常数的 题目保证最后答案小于1000 故从后往前尝试用后面的线段 "压"前面的线段 排除不可能的答案 就可以轻松AC了. #include<iostream> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> #include<algorit

POJ 2826 An Easy Problem? 判断线段相交

POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客 下面三种情况比较特殊,特别是第三种 G++怎么交都是WA,同样的代码C++A了 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const double eps = 1e-8;

hdu 1147 Pick-up sticks 判断线段相交 ~~ 注意判断顺序!!不然容易超时

Pick-up sticks Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2216    Accepted Submission(s): 815 Problem Description Stan has n sticks of various length. He throws them one at a time on the f

【POJ 1556】The Doors 判断线段相交+SPFA

黑书上的一道例题:如果走最短路则会碰到点,除非中间没有障碍. 这样把能一步走到的点两两连边,然后跑SPFA即可. #include<cmath> #include<cstdio> #include<cstring> #include<algorithm> #define N 100003 using namespace std; struct Point { double x, y; Point(double _x = 0, double _y = 0) :

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

hdu 1086(判断线段相交)

传送门:You can Solve a Geometry Problem too 题意:给n条线段,判断相交的点数. 分析:判断线段相交模板题,快速排斥实验原理就是每条线段代表的向量和该线段的一个端点与 另一条线段的两个端点构成的两个向量求叉积,如果线段相交那么另一条线段两个端点必定在该线段的两边,则该线段代表的向量必定会顺时针转一遍逆时针转一遍,叉积必定会小于等于0,同样对另一条线段这样判断一次即可. #include <algorithm> #include <cstdio>

HDU 1086 You can Solve a Geometry Problem too(判断线段相交)

题目地址:HDU 1086 就这么一道仅仅判断线段相交的题目写了2k多B的代码..是不是有点浪费...但是我觉得似乎哪里也优化不了了.... 判断线段相交就是利用的叉积.假如现在两条线段分别是L1和L2,先求L1和L2两个端点与L1的某个端点的向量的叉积,如果这两个的叉积的乘积小于0的话,说明L1在是在L2两个端点之间的,但此时并不保证一定相交.此时需要用同样的方法去判断L2是否在L1的两个端点之间,如果L2也在L1的两个端点之间的话,那就足以说明L1与L2相交.但是这题还需要判断是否端点也相交

HDU1558 - Segment set 并查集 + 判断线段相交

HDU1558 - Segment set: http://acm.hdu.edu.cn/showproblem.php?pid=1558 题目大意: 输入一些线段的端点坐标,若两线段相交,则把他们合并成一个集合,输入数据里会有k,问和k线段相交的线段的数目(包括自己) 思路: 每次输入一条线段,都从头扫描一次. 找出之前输入的线段里面,所有和其相交的线段,并且合并(合并用的是线段的ID). 就是: 并查集 + 判断线段相交 代码: #include <iostream> #include &