码迷,mamicode.com
首页 > 其他好文 > 详细

Pick-up sticks(判断两直线相交)

时间:2015-11-07 21:50:59      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

Pick-up sticks
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 11335   Accepted: 4250

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.
题解:这个题是让找出与其他直线不交或者在其他直线最上面的直线;
判断相交,两个直线的一个端点都要判断;
代码:
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #define mem(x,y) memset(x,y,sizeof(x))
 7 using namespace std;
 8 typedef long long LL;
 9 const int INF=0x3f3f3f3f;
10 const int MAXN=100010<<1;
11 struct Point{
12     double x,y;
13     Point(double x=0,double y=0):x(x),y(y){}
14 };
15 typedef Point Vector;
16 Point dt[MAXN];
17 Vector operator - (Point a,Point b){return Vector(a.x-b.x,a.y-b.y);}
18 double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}
19 int main(){
20     int n,k,j;
21     while(scanf("%d",&n),n){
22         k=0;
23         for(int i=1;i<=n;i++)
24             scanf("%lf%lf%lf%lf",&dt[i].x,&dt[i].y,&dt[i+n].x,&dt[i+n].y);
25             printf("Top sticks: ");
26         for(int i=1;i<n;i++){
27             for(j=i+1;j<=n;j++){
28                 if(Cross(dt[i]-dt[j],dt[i]-dt[j+n])*Cross(dt[i+n]-dt[j],dt[i+n]-dt[j+n])<=0)
29                     if(Cross(dt[j]-dt[i],dt[j]-dt[i+n])*Cross(dt[j+n]-dt[i],dt[j+n]-dt[i+n])<=0)
30                         break;
31             }
32             if(j==n+1)printf("%d, ",i);
33         }
34         printf("%d.\n",n); 
35     }
36     return 0;
37 }

 

Pick-up sticks(判断两直线相交)

标签:

原文地址:http://www.cnblogs.com/handsomecui/p/4946074.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!