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 = ;
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 < && d3 * d4 < )
return true;//规范相交
//下面的四个都是非规范相交
if (d1 == && on_segment(a, b, c))
return true;
if (d2 == && on_segment(a, b, d))
return true;
if (d3 == && on_segment(c, d, a))
return true;
if (d4 == && on_segment(c, d, b))
return true;
return false;
}
void check_segments(int m)
{
for (int i = m + ; 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 = ; 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 = ; i < n; i++)
check_segments(i);
printf("Top sticks: ");
bool first = true;
for (int i = ; i < n; i++)
{
if (!vis[i])
{
if (first)
first = false;
else
printf(", ");
printf("%d", i + );
}
}
printf(".\n");
} return ;
}
 

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

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

    一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...

  2. POJ2653 Pick-up sticks 判断线段相交

    POJ2653 判断线段相交的方法 先判断直线是否相交 再判断点是否在线段上 复杂度是常数的 题目保证最后答案小于1000 故从后往前尝试用后面的线段 "压"前面的线段 排除不可能 ...

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

    POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客 下面三种情况比较特殊,特别是第三种 G++怎么交都是WA,同样的代码C++A了 #include <io ...

  4. POJ 1066 - Treasure Hunt - [枚举+判断线段相交]

    题目链接:http://poj.org/problem?id=1066 Time Limit: 1000MS Memory Limit: 10000K Description Archeologist ...

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

    黑书上的一道例题:如果走最短路则会碰到点,除非中间没有障碍. 这样把能一步走到的点两两连边,然后跑SPFA即可. #include<cmath> #include<cstdio> ...

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

    Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 7699   Accepted: 2843 De ...

  7. POJ 1066--Treasure Hunt(判断线段相交)

    Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7857   Accepted: 3247 Des ...

  8. POJ_2653_Pick-up sticks_判断线段相交

    POJ_2653_Pick-up sticks_判断线段相交 Description Stan has n sticks of various length. He throws them one a ...

  9. 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)

    传统解法 题目来自 leetcode 335. Self Crossing. 题意非常简单,有一个点,一开始位于 (0, 0) 位置,然后有规律地往上,左,下,右方向移动一定的距离,判断是否会相交(s ...

随机推荐

  1. ubuntu下使用C语言开发一个cgi程序

    主要步骤是: 1. 开发一个C程序(在标准输出中输出HTML字符串) 2. 复制到apache2的cgi-bin目录去 3. 在httpd.conf中开启cgi功能(我似乎没用到,也可以使用cgi) ...

  2. 转:windows下使用gvim搭建简单的IDE编译环境(支持C/C++/Python等)

    原文来自于:http://www.cnblogs.com/zhuyp1015/archive/2012/06/16/2552269.html 使用gvim在windows环境下搭建简单的IDE环境可以 ...

  3. 求助:对话框下OnInitDialog中使用SetTimer无效

    原文地址:http://www.w3c.com.cn/%E6%B1%82%E5%8A%A9%EF%BC%9A%E5%AF%B9%E8%AF%9D%E6%A1%86%E4%B8%8Boninitdial ...

  4. 均值,方差: 概率质量函数PMF

    __author__ = 'dell' import Pmf import matplotlib.pyplot as pyplot pmf = Pmf.MakePmfFromList([1, 2, 2 ...

  5. unity3d中的http通信 二

    转载自 http://www.cnblogs.com/88999660/archive/2013/03/11/2954279.html 如果侵权,请及时通知我删除! using System; usi ...

  6. Catenyms

    poj2337:http://poj.org/problem?id=2337 题意:给定一些单词,如果一个单词的尾字母与另一个的首字母相同则可以连接.问是否可以每个单词用一次,将所有单词连接,可以则输 ...

  7. PGA突破pga_aggregate_target限制

    SQL> show parameter pga NAME         TYPE  VALUE ------------------------------------ ----------- ...

  8. Gvim各种插件配置(windows环境下)

    1.Vundle插件:https://github.com/gmarik/Vundle.vim 用于管理Vim插件,安装此插件需要系统中已安装git,参考链接:Git for Windows安装和基本 ...

  9. bzoj2588

    一开始一看树上的操作,就无脑写了树链剖分+主席树 然后果断T了,因为树链剖分+主席树必然带来两个log的复杂度 而且树链剖分复杂度还比较大…… 后来发现其实没必要,在这道题,我们可以直接利用主席树维护 ...

  10. 今天愉快的hack小记

    今天发生了一件很好玩的事情...那就是WZJ的数据结构(负五)被人水掉了...用的是线段树暴力大发好... XYZ折腾了多长时间的论文题就这么被搞掉了...?窝来维护正义了! 怎么卡呢:让线段树走到叶 ...