​Pick-up sticks POJ - 2653​​​
分类:计算几何+线段相交**
题目:
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.

题意:按顺序扔下棍子,求最上面的棍子是哪些。
思路:此题就是判断线段相交。第一次是边输入往后判断它在哪一层超时了。后改用直接重最底层的开始往上判断,只要与上面相交的就肯定不是答案。
AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=100000+10;
struct point
{
double x,y;
point(){}
point(double _x,double _y){x=_x;y=_y;}
};
struct egde
{
point start,end;
egde(){}
egde(point a,point b){
start=a;end=b;}

}edges[N];
double multi(point p1,point p2,point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool intAcross(egde v1,egde v2)
{
if(max(v1.start.x,v1.end.x)>=min(v2.start.x,v2.end.x)&&
max(v2.start.x,v2.end.x)>=min(v1.start.x,v1.end.x)&&
max(v1.start.y,v1.end.y)>=min(v2.start.y,v2.end.y)&&
multi(v2.start,v1.end,v1.start)*multi(v1.end,v2.end,v1.start)>0&&
multi(v1.start,v2.end,v2.start)*multi(v2.end,v1.end,v2.start)>0) return 1;//相交
return 0;
}//判断相交
int main()
{
int n;double a,b,c,d;
while(scanf("%d",&n)!=EOF&&n)
{
for(int i=0;i<n;i++)
{
scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
edges[i]=egde(point(a,b),point(c,d));
}
printf("Top sticks: ");
for(int i=0;i<n-1;i++)
{
int j=i+1;
for(j=i+1;j<n;j++)
{
if(intAcross(edges[i],edges[j])) break;
}
if(j==n) printf("%d, ",i+1);
}
printf("%d.\n",n);//最后一个肯定在上面
}
return 0;