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

Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 7404 Accepted: 2729

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.

思路:一开始根据输入往前搜,TLE了。然后换成先把每条棍子的数据存起来,从头往后搜,遇到相交,说明该棍子不在top里,用bool数组标记一下,然后break掉。就500ms过了。此题的收获是:暴搜也是有讲究的。

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #define MAX 100005
 6 
 7 using namespace std;
 8 
 9 struct point
10 {
11     double x,y;
12 };
13 
14 struct node
15 {
16     point start,end;
17 };
18 
19 node s[MAX];
20 bool top[MAX];
21 
22 double mul(point p2,point p1,point p0)
23 {
24     return ((p1.x-p0.x)*(p2.y-p0.y) - (p2.x-p0.x)*(p1.y-p0.y));
25 }
26 
27 bool isIntersect(node s1,node s2)
28 {
29     if(min(s1.start.x,s1.end.x)<=max(s2.start.x,s2.end.x)&&
30             min(s2.start.x,s2.end.x)<=max(s1.start.x,s1.end.x)&&
31             min(s1.start.y,s1.end.y)<=max(s2.start.y,s2.end.y)&&
32             min(s2.start.y,s2.end.y)<=max(s1.start.y,s1.end.y)&&
33             mul(s1.end,s2.start,s1.start)*mul(s2.end,s1.end,s1.start)>=0&&
34             mul(s2.end,s1.end,s2.start)*mul(s1.start,s2.end,s2.start)>=0)
35         return true;
36     return false;
37 }
38 
39 int N;
40 
41 int main()
42 {
43     while(~scanf("%d",&N) && N)
44     {
45         memset(top,1,sizeof(top));
46         for(int i=1; i<=N; i++)
47             scanf("%lf%lf%lf%lf",&s[i].start.x,&s[i].start.y,&s[i].end.x,&s[i].end.y);
48         for(int i=1; i<N; i++)
49         {
50             for(int j=i+1; j<=N; j++)
51             {
52                 if(isIntersect(s[i],s[j]))
53                 {
54                     top[i] = false;
55                     break;
56                 }
57             }
58         }
59         printf("Top sticks: ");
60         for(int i=1; i<N; i++)
61             if(top[i])
62                 printf("%d, ",i);
63         printf("%d.\n",N);
64     }
65     return 0;
66 }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值