Pick-up sticks

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pick-up sticks相关的知识,希望对你有一定的参考价值。

Pick-up sticks

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.
 

题目大意,先后给出n条直线,求最后哪些直线上没有直线.

那么,由于n最大100000,所有肯定不能用普通的来.

其实,我们只要记录最上面的几条直线就行了,并且可以用动态数组来.毕竟只有100000条直线,却有可能有n个集合,也有可能只有1个,所有,用vector代替普通的数组是再好不过的了.

然而,数据实在太强,还是TLE了.然而...暴力加break优化却AC了!!!

技术分享
 1 #include<cmath>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<vector>
 6 #define Vec point
 7 using namespace std;
 8 const double eps=1e-8;
 9 vector <int> que;
10 int n;
11 bool f[100005];
12 struct point{
13     double x,y;
14     void read(){scanf("%lf%lf",&x,&y);}
15 }seg[100005][2];
16 int fabso(double x){return x<-eps?-1:x>eps;}
17 Vec operator + (point u,Vec v){
18     Vec ret; ret.x=u.x+v.x,ret.y=u.y+v.y;
19     return ret;
20 }
21 Vec operator - (point u,point v){
22     Vec ret; ret.x=u.x-v.x,ret.y=u.y-v.y;
23     return ret;
24 }
25 Vec operator * (Vec u,double v){
26     Vec ret; ret.x=u.x*v,ret.y=u.y*v;
27     return ret;
28 }
29 Vec operator / (Vec u,double v){
30     Vec ret; ret.x=u.x/v,ret.y=u.y/v;
31     return ret;
32 }
33 double cross(Vec u,Vec v){return u.x*v.y-u.y*v.x;}
34 bool dotonseg(point P,point U,point V){
35     if ((P.x-U.x)*(P.x-V.x)>0) return 0;
36     if ((P.y-U.y)*(P.y-V.y)>0) return 0;
37     return cross(P-U,V-U)==0;
38 }
39 bool intersect(point P,point Q,point U,point V){
40     if (fabso(cross(P-U,V-U)*cross(Q-U,V-U))<0&&fabso(cross(U-P,Q-P)*cross(V-P,Q-P))<0) return 1;
41     bool g1=dotonseg(P,U,V);
42     bool g2=dotonseg(Q,U,V);
43     bool g3=dotonseg(U,P,Q);
44     bool g4=dotonseg(V,P,Q);
45     if (g1||g2||g3||g4) return 1;
46     return 0;
47 }
48 int main(){
49     for (scanf("%d",&n); n; scanf("%d",&n)){
50         memset(f,1,sizeof f);
51         for (int i=1; i<=n; i++) seg[i][0].read(),seg[i][1].read();
52         for (int i=1; i<=n-1; i++)
53             for (int j=i+1; j<=n; j++)
54             if (intersect(seg[i][0],seg[i][1],seg[j][0],seg[j][1])){f[i]=0; break;}
55         printf("Top sticks:");
56         for (int i=1; i<n; i++) if (f[i]) printf(" %d,",i);
57         printf(" %d.\n",n);
58     }
59     return 0;
60 } 
View Code

 



以上是关于Pick-up sticks的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2653 Pick-up sticks

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

POJ 2653 Pick-up sticks(几何)

POJ2653 Pick-up sticks(线段相交)

POJ 2653 Pick-up sticks [计算几何,线段相交]

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