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

Pick-up sticks

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 11537   Accepted: 4337

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.

Source

Waterloo local 2005.09.17

【思路】

暴力枚举+判断线段是否相交。

数据弱得一 哔~  <_<

【代码】

 1 #include<cmath>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
 6 using namespace std;
 7
 8 const int N = 100000+10;
 9 const double eps = 1e-8;
10 int dcmp(double x) {
11     if(x<eps) return 0; else return x<0? -1:1;
12 }
13
14 struct Pt {
15     double x,y;
16     Pt (double x=0,double y=0):x(x),y(y) {};
17 };
18 struct Line { Pt a1,a2;
19 };
20 typedef Pt vec;
21 vec operator - (Pt A,Pt B) { return vec(A.x-B.x,A.y-B.y); }
22
23 double cross(vec A,vec B) { return A.x*B.y-A.y*B.x; }
24
25 bool SegInter(Pt s1, Pt e1, Pt s2, Pt e2) {
26     if( min(s1.x, e1.x) <= max(s2.x, e2.x) &&
27         min(s1.y, e1.y) <= max(s2.y, e2.y) &&
28         min(s2.x, e2.x) <= max(s1.x, e1.x) &&
29         min(s2.y, e2.y) <= max(s1.y, e1.y) &&
30         cross(e1-s1,s2-s1) * cross(e1-s1,e2-s1) <= 0 &&
31         cross(e2-s2,s1-s2) * cross(e2-s2,e1-s2) <= 0
32       ) return true;
33     return false;
34 }
35
36 int n;
37 Line L[N];
38
39 int main() {
40     while(scanf("%d",&n)==1 && n) {
41         double x,y,x2,y2;
42         FOR(i,1,n) {
43             scanf("%lf%lf%lf%lf",&x,&y,&x2,&y2);
44             L[i].a1=Pt(x,y) , L[i].a2=Pt(x2,y2);
45         }
46         bool first=1;
47         printf("Top sticks:");
48         FOR(i,1,n) {
49             bool flag=1;
50             FOR(j,i+1,n)
51                 if(SegInter(L[i].a1,L[i].a2,L[j].a1,L[j].a2)) {
52                     flag=0; break;
53                 }
54             if(flag) {
55                 if(first) first=0; else putchar(‘,‘);
56                 printf(" %d",i);
57             }
58         }
59         puts(".");
60     }
61     return 0;
62 }
时间: 2024-08-09 00:07:18

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

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 fin

【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 2653 Pick-up sticks(几何线段相交判断)

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10949   Accepted: 4085 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条线段依次放到二维平面上,问最后有哪些没与前面的线段相交,即它是顶上的线段 题解:数据弱,正向纯模拟可过 但是有一个陷阱:如果我们从后面向前枚举,找与前面哪些相交,再删除前面那些相交的线段,这样就错了 因为如果线段8与5,6,7相交了,我们接下来不能直接判断4,我们还要找7,6,5与之前哪些相交 #include<set> #include<map> #include<queue> #include<stack> #include<cmat

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

题意:n根木棍随意摆放在一个平面上,问放在最上面的木棍是哪些. 思路:线段相交,因为题目说最多有1000根在最上面.所以从后往前处理,直到木棍没了或者最上面的木棍的总数大于1000. #include<stdio.h> #include<string.h> #include<math.h> #include<iostream> using namespace std; const int N=1e5+111; const double eps=1e-8; i

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

题目链接 题意 : 把每根棍往地上扔,找出最后在上面的棍,也就是说找出所有的没有别的棍子压在它的上面的棍子. 思路 : 对于每根棍子,压在他上面的棍子一定是在它之后扔的棍子,所以在找的时候只要找它之后的线段是否与他相交即可. 1 //2653 2 #include <stdio.h> 3 #include <iostream> 4 #include <math.h> 5 #include <string.h> 6 7 using namespace std

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 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;

POJ 1269 Intersecting Lines(线段相交,水题)

Intersecting Lines 大意:给你两条直线的坐标,判断两条直线是否共线.平行.相交,若相交,求出交点. 思路:线段相交判断.求交点的水题,没什么好说的. struct Point{ double x, y; } ; struct Line{ Point a, b; } A, B; double xmult(Point p1, Point p2, Point p) { return (p1.x-p.x)*(p2.y-p.y)-(p1.y-p.y)*(p2.x-p.x); } bool