Pick-up sticks
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 14420 | Accepted: 5463 |
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.
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.
Hint
Huge input,scanf is recommended.
Source
暴力找每一个线段上面是否有和它相交的线段就行,本来看数据量还以为会超时呢。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
const double eps = 1e-8;
const int maxn = 100005;
struct point{
double xx, yy;
point(double x=0,double y=0):xx(x),yy(y) {};
};
struct segment{
point a, b;
};
double min(double A, double B) { return A < B ? A : B; }
double max(double A, double B) { return A > B ? A : B; }
double Cross(point A, point B) { return A.xx * B.yy - A.yy * B.xx; }//叉乘
int dcmp(double x){
if(fabs(x) < eps) return 0;
return x < 0 ? -1 : 1;
}
point operator + (point A, point B) {return point(A.xx + B.xx, A.yy + B.yy); }
point operator - (point A, point B) {return point(A.xx - B.xx, A.yy - B.yy); }
point operator * (point A, double p) {return point(A.xx * p, A.yy * p); }
point operator / (point A, double p) {return point(A.xx / p, A.yy / p); }
bool operator == (point A, point B) {return dcmp(A.xx - B.xx) == 0 && dcmp(A.yy - B.yy) == 0; }
bool Exclusion(segment p, segment q){//快速排斥试验
if(min(p.a.xx, p.b.xx) - max(q.a.xx, q.b.xx) > eps || min(p.a.yy, p.b.yy) - max(q.a.yy, q.b.yy) > eps || min(q.a.xx, q.b.xx) - max(p.a.xx, p.b.xx) > eps || min(q.a.yy, q.b.yy) - max(p.a.yy, p.b.yy) > eps )
return 0;
return 1;
}
bool Straddling(segment p, segment q){//跨立实验
if(dcmp(Cross(p.b - p.a, q.a - p.a)) * dcmp(Cross(p.b - p.a, q.b - p.a)) <= 0 && dcmp(Cross(q.b - q.a, p.a - q.a)) * dcmp(Cross(q.b - q.a, p.b - q.a)) <= 0 )
return 1;
return 0;
}
bool check(segment p, segment q){//判断两线段是否相交
if(Exclusion(p, q) && Straddling(p,q))
return 1;
return 0;
}
/*--------------------------------------------------------------------------------------------------------------------------------*/
segment s[maxn];
int main(){
int n;
while(scanf("%d", &n) && n){
for(int i = 1; i <= n; i++){
scanf("%lf%lf%lf%lf", &s[i].a.xx, &s[i].a.yy, &s[i].b.xx, &s[i].b.yy);
}
printf("Top sticks: ");
for(int i = 1,j; i < n; i++){
for(j = i+1; j <= n; j++)
if(check(s[i], s[j]))
break;
if(j == n + 1)
printf("%d, ", i);
}
printf("%d.\n", n);
}
return 0;
}