Test instructions: N sticks are randomly placed on a flat surface, asking what sticks are on top.
Idea: line intersection, because the topic said there are up to 1000 at the top. So go from back to front until the stick is gone or the top stick has a total of more than 1000.
#include <stdio.h>#include<string.h>#include<math.h>#include<iostream>using namespacestd;Const intn=1e5+111; Const Doubleeps=1e-8;intSgnDoublex) { if(Fabs (x) <eps)return 0; if(x>0)return 1; return-1;}structpoint{Doublex, y; Point () {}, point (DoubleX_Doubley_) {x=x_,y=Y_; } Pointoperator-(ConstPoint &b)Const{ returnPoint (x-b.x,y-b.y); } Double operator*(ConstPoint &b)Const{ returnx*b.x+y*b.y; } Double operator^(ConstPoint &b)Const{ returnx*b.y-y*b.x; }};structline{Point S,e; Line () {} line (point S_,point e_) {s=s_,e=E_; }}li[n];DoubleCal (Point p0,point p1,point p2) {//Cross Product return(p1-p0) ^ (p2-p0);}intXJ (line A,line b) {//determine if two segments intersectPoint a=a.s,b=a.e,c=b.s,d=B.E; returnMax (a.x,b.x)>=min (c.x,d.x) &&Max (c.x,d.x)>=min (a.x,b.x) &&Max (A.Y,B.Y)>=min (C.Y,D.Y) &&Max (C.Y,D.Y)>=min (A.Y,B.Y) &&SGN (Cal (a,c,d))*SGN (Cal (b,c,d)) <=0&&SGN (Cal (c,a,b))*SGN (Cal (d,a,b)) <=0;}intAns[n];intMain () {intN,i,j,js; while(~SCANF ("%d", &n) &&N) { Doublex1,x2,y1,y2;js=0; for(i=1; i<=n;i++) {scanf ("%LF%LF%LF%LF",&x1,&y1,&x2,&y2); Li[i]=Line (Point (X1,y1), point (X2,y2)); } for(i=n;i&&js< +; i--){ for(j=i+1; j<=n;j++){ if(XJ (LI[I],LI[J))) Break; } if(j>n) ans[++js]=i; } printf ("Top Sticks:"); for(i=js;i;i--) {printf ("%d%c", ans[i],i==1?'.':','); } puts (""); } return 0;}
POJ 2653 Pick-up Sticks "segment intersection"