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.
分析: 一开始从后往前枚举,如果相交,就把前面的线段标记掉,结果TLE, 然后改成从前往后枚举,如果相交标记前面的这条边并Break;
结果400多毫秒k掉了。


#include<stdio.h>
#include<string.h>
#define clr(x) memset(x,0,sizeof(x))
int v[100005];
double min(double a,double b)
{
if(a<b)
return a;
return b;
}
double max(double a,double b)
{
if(a>b)
return a;
return b;
}
struct node
{
double x1,y1,x2,y2;
}q[100005];
double cha(double x1,double y1,double x2,double y2)
{
return x1*y2-y1*x2; //叉积
}
int kua(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
if(min(x1,x2)>max(x3,x4)||min(y1,y2)>max(y3,y4)||max(x1,x2)<min(x3,x4)||max(y1,y2)<min(y3,y4))
return 0; //排斥实验
if((cha(x3-x4,y3-y4,x1-x4,y1-y4)*cha(x3-x4,y3-y4,x2-x4,y2-y4)<=0)&&(cha(x1-x2,y1-y2,x3-x2,y3-y2)*cha(x1-x2,y1-y2,x4-x2,y4-y2)<=0)) //跨立实验
return 1;
else return 0;
}
int p(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)
{
if(kua(x1,y1,x2,y2,x3,y3,x4,y4)&&kua(x3,y3,x4,y4,x1,y1,x2,y2))
return 1;
return 0;
}
int main()
{
int n,i,j,top;
while(scanf("%d",&n),n)
{
top=0;
clr(v);
for(i=0;i<n;i++)
scanf("%lf%lf%lf%lf",&q[i].x1,&q[i].y1,&q[i].x2,&q[i].y2);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(p(q[i].x1,q[i].y1,q[i].x2,q[i].y2,q[j].x1,q[j].y1,q[j].x2,q[j].y2))
{ v[i]=1;break;}
printf("Top sticks: ");
for(i=n-1;i>=0;i--)
if(!v[i])break;
for(j=0;j<i;j++)
if(!v[j])
printf("%d, ",j+1);
printf("%d.\n",i+1);
}
return 0;
}