POJ

来源:互联网 发布:保卫萝卜安卓数据同步 编辑:程序博客网 时间:2024/06/05 16:04

POJ - 2653: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
51 1 4 22 3 3 11 -2.0 8 41 4 8 23 3 6 -2.030 0 1 11 0 2 12 0 3 10
Sample Output
Top sticks: 2, 4, 5.Top sticks: 1, 2, 3.
Hint
Huge input,scanf is recommended.

思路:每次放了一根木棒后,先把被这木棒压住的木棒从队列中拿走,然后把这木棒加入到队列。(考虑到只有端点相交的情况,2根木棒如果只有端点重合,不算相交,题目没提到这种情况,可能数据没这样的情况,或者我这样的说法是对的)

#include<iostream>#include<cstdio>#include<queue>#include<cstring>using namespace std;struct line{double sx,sy,ex,ey;int index;}p[100010];struct Point{double x,y;};double cross(Point a,Point b)       //叉积{return (a.x*b.y-a.y*b.x);}double dot(Point a,Point b)        //点积{return (a.x*b.x+a.y*b.y);}Point Vector(Point a,Point b)     //求向量ba {Point c;c.x=a.x-b.x;c.y=a.y-b.y;return c;}bool check(line &a,line& b)       //判断线段a,b是否相交 {Point a1,a2,b1,b2;a1.x=a.sx,a1.y=a.sy;a2.x=a.ex,a2.y=a.ey;b1.x=b.sx,b1.y=b.sy;b2.x=b.ex,b2.y=b.ey;double c1=cross(Vector(a2,a1),Vector(b1,a1)),c2=cross(Vector(a2,a1),Vector(b2,a1));double c3=cross(Vector(b2,b1),Vector(a1,b1)),c4=cross(Vector(b2,b1),Vector(a2,b1));return c1*c2<0&&c3*c4<0;}int main(){int n;while(scanf("%d",&n)!=EOF&&n){queue<struct line>a,b;for(int i=0;i<n;i++){scanf("%lf%lf%lf%lf",&p[i].sx,&p[i].sy,&p[i].ex,&p[i].ey);p[i].index=i+1;if(!a.empty()){while(!a.empty()){line now=a.front();a.pop();if(check(p[i],now)==0)b.push(now);}a=b;while(!b.empty())b.pop();}a.push(p[i]);} printf("Top sticks:");while(!a.empty()){printf(" %d",a.front().index);a.pop();if(a.empty())printf(".\n");else printf(",");}}return 0;}