POJ 2653 Pick-up sticks(线段判交)
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.
The picture to the right below illustrates the first case from input.
题目大意:在地上抛n条棍子,问有哪几条棍子没有被其他棍子压着。
思路:暴力搜,这题暂时还没有发现什么有效的解法……
有人说You may assume that there are no more than 1000 top sticks.所以换个姿势暴力就不会TLE了……
明明最坏还是得O(n^2)嘛……只要你给我代码还是能卡……比如我的代码……前n-1个线段都不相交,最后一条跨立前n-1个线段,然后每次都要扫到最后,就卡掉了……
除非上面那句话说的是任何时刻而不是最后时刻,嘛没有说清楚这就是出题人的问题了……(在线计算,拿个1000的队列记住那些还没被覆盖,复杂度O(n * 1000))
反正我只是为做模板而已……随便啦这种小事……
代码(516MS):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 8 const double EPS = 1e-8; 9 10 inline int sgn(double x) { 11 return (x > EPS) - (x < -EPS); 12 } 13 14 struct Point { 15 double x, y; 16 Point() {} 17 Point(double x, double y): x(x), y(y) {} 18 void read() { 19 scanf("%lf%lf", &x, &y); 20 } 21 bool operator < (const Point &rhs) const { 22 if(y != rhs.y) return y < rhs.y; 23 return x < rhs.x; 24 } 25 Point operator + (const Point &rhs) const { 26 return Point(x + rhs.x, y + rhs.y); 27 } 28 Point operator - (const Point &rhs) const { 29 return Point(x - rhs.x, y - rhs.y); 30 } 31 Point operator * (const int &b) const { 32 return Point(x * b, y * b); 33 } 34 Point operator / (const int &b) const { 35 return Point(x / b, y / b); 36 } 37 double length() const { 38 return sqrt(x * x + y * y); 39 } 40 Point unit() const { 41 return *this / length(); 42 } 43 }; 44 typedef Point Vector; 45 46 double dist(const Point &a, const Point &b) { 47 return (a - b).length(); 48 } 49 50 double across(const Point &a, const Point &b) { 51 return a.x * b.y - a.y * b.x; 52 } 53 //ret >= 0 means turn left 54 double cross(const Point &sp, const Point &ed, const Point &op) { 55 return sgn(across(sp - op, ed - op)); 56 } 57 58 struct Seg { 59 Point st, ed; 60 Seg() {} 61 Seg(Point st, Point ed): st(st), ed(ed) {} 62 void read() { 63 st.read(); ed.read(); 64 } 65 }; 66 67 bool isIntersected(Point s1, Point e1, Point s2, Point e2) { 68 return (max(s1.x, e1.x) >= min(s2.x, e2.x)) && 69 (max(s2.x, e2.x) >= min(s1.x, e1.x)) && 70 (max(s1.y, e1.y) >= min(s2.y, e2.y)) && 71 (max(s2.y, e2.y) >= min(s1.y, e1.y)) && 72 (cross(s2, e1, s1) * cross(e1, e2, s1) >= 0) && 73 (cross(s1, e2, s2) * cross(e2, e1, s2) >= 0); 74 } 75 76 bool isIntersected(Seg a, Seg b) { 77 return isIntersected(a.st, a.ed, b.st, b.ed); 78 } 79 80 /*******************************************************************************************/ 81 82 const int MAXN = 100010; 83 84 Seg s[MAXN]; 85 bool isAns[MAXN]; 86 Point p; 87 int n; 88 89 int main() { 90 while(scanf("%d", &n) != EOF && n) { 91 memset(isAns, true, sizeof(isAns)); 92 for(int i = 0; i < n; ++i) s[i].read(); 93 for(int i = 0; i < n; ++i) 94 for(int j = i + 1; j < n; ++j) { 95 if(isIntersected(s[i], s[j])) { 96 isAns[i] = false; 97 break; 98 } 99 } 100 bool flag = false; 101 printf("Top sticks:"); 102 for(int i = 0; i < n; ++i) { 103 if(!isAns[i]) continue; 104 if(flag) printf(","); 105 else flag = true; 106 printf(" %d", i + 1); 107 } 108 puts("."); 109 } 110 }