Source Code
Problem: 2653 | User: Praesidio | |
Memory: 3320K | Time: 610MS | |
Language: C++ | Result: Accepted |
- Source Code
//poj 2653 #include <cstdio> #include <cmath> #include <iostream> #include <vector> using namespace std; #define sqr(x) ((x) * (x)) const double eps = 1e-9; const double pi = acos(-1.0); int sgn(const double &a) { return (a > eps) - (a < -eps); } struct Point { double x,y; Point () {} Point (double x,double y) : x(x),y(y) {} }; typedef Point Vector ; Vector operator - (Point A,Point B) {return Vector (A.x-B.x,A.y-B.y); } double operator ^ (Vector A, Vector B) {return A.x*B.x + A.y*B.y;} double operator * (Vector A, Vector B) {return A.x*B.y - A.y*B.x;} bool operator == (const Point& a,const Point& b) { return sgn(a.x-b.x)==0 && sgn(a.y-b.y)==0; } double cross(Point a,Point b,Point c){ return (b-a)*(c-a); } int betweenCmp(Point a,Point b,Point c) { return sgn((b-a)^(c-a)); } int segcross(Point a,Point b,Point c,Point d,Point& p) { double s1=cross(a,b,c),s2=cross(a,b,d),s3=cross(c,d,a),s4=cross(c,d,b); int d1=sgn(s1),d2=sgn(s2),d3=sgn(s3),d4=sgn(s4); if ( (d1^d2)==-2 && (d3^d4)==-2 ) { p.x = (c.x*s2 - d.x*s1) / (s2-s1) ; p.y = (c.y*s2 - d.y*s1) / (s2-s1) ; return 1; } if (d1==0 && betweenCmp(c,a,b)<=0 || d2==0 && betweenCmp(d,a,b)<=0 || d3==0 && betweenCmp(a,c,d)<=0 || d4==0 && betweenCmp(b,c,d)<=0 ) return 2; return 0; } //========== struct Seg{ Point a,b; Seg () {} Seg (Point a,Point b) : a(a),b(b) { } }; Seg line[100002]; int n; vector <int> ans; void init() { for (int i=1;i<=n;i++) { double x1,x2,y1,y2; scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); line[i]=Seg(Point(x1,y1) , Point(x2,y2)); } } int main() { while (~scanf("%d",&n)&&n) { init(); ans.clear(); for (int i=n;i>=1;i--) { int flag=1; for (int j=i+1;j<=n;j++) { Point p; if (segcross(line[i].a,line[i].b,line[j].a,line[j].b,p)==1) { flag=0; break; } } if (flag) ans.push_back(i); } printf("Top sticks:"); for (int i=ans.size()-1;i>0;i--) printf(" %d,",ans[i]); printf(" %d.\n",ans[0]); } return 0; }