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.
题目大意:在地上抛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):
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std; const double EPS = 1e-; inline int sgn(double x) {
return (x > EPS) - (x < -EPS);
} struct Point {
double x, y;
Point() {}
Point(double x, double y): x(x), y(y) {}
void read() {
scanf("%lf%lf", &x, &y);
}
bool operator < (const Point &rhs) const {
if(y != rhs.y) return y < rhs.y;
return x < rhs.x;
}
Point operator + (const Point &rhs) const {
return Point(x + rhs.x, y + rhs.y);
}
Point operator - (const Point &rhs) const {
return Point(x - rhs.x, y - rhs.y);
}
Point operator * (const int &b) const {
return Point(x * b, y * b);
}
Point operator / (const int &b) const {
return Point(x / b, y / b);
}
double length() const {
return sqrt(x * x + y * y);
}
Point unit() const {
return *this / length();
}
};
typedef Point Vector; double dist(const Point &a, const Point &b) {
return (a - b).length();
} double across(const Point &a, const Point &b) {
return a.x * b.y - a.y * b.x;
}
//ret >= 0 means turn left
double cross(const Point &sp, const Point &ed, const Point &op) {
return sgn(across(sp - op, ed - op));
} struct Seg {
Point st, ed;
Seg() {}
Seg(Point st, Point ed): st(st), ed(ed) {}
void read() {
st.read(); ed.read();
}
}; bool isIntersected(Point s1, Point e1, Point s2, Point e2) {
return (max(s1.x, e1.x) >= min(s2.x, e2.x)) &&
(max(s2.x, e2.x) >= min(s1.x, e1.x)) &&
(max(s1.y, e1.y) >= min(s2.y, e2.y)) &&
(max(s2.y, e2.y) >= min(s1.y, e1.y)) &&
(cross(s2, e1, s1) * cross(e1, e2, s1) >= ) &&
(cross(s1, e2, s2) * cross(e2, e1, s2) >= );
} bool isIntersected(Seg a, Seg b) {
return isIntersected(a.st, a.ed, b.st, b.ed);
} /*******************************************************************************************/ const int MAXN = ; Seg s[MAXN];
bool isAns[MAXN];
Point p;
int n; int main() {
while(scanf("%d", &n) != EOF && n) {
memset(isAns, true, sizeof(isAns));
for(int i = ; i < n; ++i) s[i].read();
for(int i = ; i < n; ++i)
for(int j = i + ; j < n; ++j) {
if(isIntersected(s[i], s[j])) {
isAns[i] = false;
break;
}
}
bool flag = false;
printf("Top sticks:");
for(int i = ; i < n; ++i) {
if(!isAns[i]) continue;
if(flag) printf(",");
else flag = true;
printf(" %d", i + );
}
puts(".");
}
}