HDUOJ Pick-up sticks 判断线段相交

it2024-10-14  18

Pick-up sticks

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1771    Accepted Submission(s): 672

Problem 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.  

 

Source University of Waterloo Local Contest 2005.09.17   #define DeBUG #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <vector> #include <stack> #include <list> #include <queue> #include <string> #include <set> #include <sstream> #include <map> #include <bitset> using namespace std ; #define zero {0} #define INF 2000000000 #define EPS 1e-6 typedef long long LL; const double PI = acos(-1.0); inline int sgn(double x){return fabs(x) < EPS ? 0 :(x < 0 ? -1 : 1);} struct Point { double x, y;//点对应坐标 Point() {} Point(double x, double y):x(x), y(y) {}//使用两点进行初始化 } ; typedef Point Vec; Vec operator + (Vec a, Vec b)//点加法 { return Vec(a.x + b.x, a.y + b.y); } Vec operator - (Vec a, Vec b)//点减法 { return Vec(a.x - b.x, a.y - b.y); } Vec operator * (Vec a, double p)//点与常数相乘 { return Vec(a.x * p, a.y * p); } Vec operator / (Vec a, double p)//点除以常数 { return Vec(a.x / p, a.y / p); } bool operator < (Point a, Point b)//平面直角坐标系中左下方的为小 { return a.x < b.x || (a.x == b.x && a.y < b.y); } bool operator == (Point a, Point b)//点相等判断 { return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0; } inline double crossDet(Vec a, Vec b)//叉乘 { return a.x * b.y - a.y * b.x; } inline double crossDet(Point o, Point a, Point b)//向量叉乘 { return crossDet(a - o, b - o); } struct Line { Point s, t;//保存线上两点 int NO; Line() {} Line(Point s, Point t) :s(s), t(t) {}//使用两点进行初始化 Point point(double x)//沿着向量st移动x个单位得到的点 { return s + (t - s) *x; } Vec vec()//得到向量st { return t - s; } } ; typedef Line Seg;//重定义线段类 inline double dotDet(Vec a, Vec b)//点乘 { return a.x * b.x + a.y * b.y; } inline bool onLine(Point x, Point a, Point b)//叉积为0三点共线 { return sgn(crossDet(a - x, b - x)) == 0; } inline bool onLine(Point x, Line l)//判断点在线上 { return onLine(x, l.s, l.t); } inline bool onSeg(Point x, Point a, Point b)//判断点在线段ab上,加上||x==a||x==b在端点也算 { return sgn(crossDet(a - x, b - x)) == 0 && sgn(dotDet(a - x, b - x)) < 0||x==a||x==b; } inline bool onSeg(Point x, Seg s)//跟上边的一样,看着舒服用的。。。 { return onSeg(x, s.s, s.t); } // 0 : not intersect // 1 : proper intersect // 2 : improper intersect int segIntersect(Point a, Point c, Point b, Point d)//线段相交判断,返回2是一条线段一端在另一线段上 { Vec v1 = b - a, v2 = c - b, v3 = d - c, v4 = a - d; int a_bc = sgn(crossDet(v1, v2)); int b_cd = sgn(crossDet(v2, v3)); int c_da = sgn(crossDet(v3, v4)); int d_ab = sgn(crossDet(v4, v1)); if(a_bc *c_da > 0 && b_cd *d_ab > 0) return 1; if(onSeg(b, a, c) && c_da) return 2; if(onSeg(c, b, d) && d_ab) return 2; if(onSeg(d, c, a) && a_bc) return 2; if(onSeg(a, d, b) && b_cd) return 2; return 0; } inline int segIntersect(Seg a, Seg b)//同上 { return segIntersect(a.s, a.t, b.s, b.t); } Seg S; template <class T> class is_itersect: public std::unary_function<T, bool> { public: bool operator()(T &val) { if(segIntersect(val,S)==0) return 0; else { return 1; } } }; Seg s[100005]; int main() { #ifdef DeBUGs freopen("C:\\Users\\Sky\\Desktop\\1.in","r",stdin); #endif int n; while(scanf("%d",&n),n) { list<Seg> L; list<Seg>::iterator it; int i,j,k; for(i=0;i<n;i++) { scanf("%lf%lf%lf%lf",&(s[i].s.x),&(s[i].s.y),&(s[i].t.x),&(s[i].t.y)); s[i].NO=i+1; S=s[i]; L.remove_if(is_itersect<Seg>()); L.push_back(S); } printf("Top sticks:"); it=L.begin(); printf(" %d",(*it).NO); for(it++;it!=L.end();it++) { printf(", %d",(*it).NO); } printf(".\n"); } return 0; } View Code

用了list实现,顺便学习一下这个用法

转载于:https://www.cnblogs.com/Skyxj/p/3364104.html

最新回复(0)