poj 2653 Pick-up sticks

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.

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.

题意:给你n个线段,问没有被覆盖的线段的编号

思路:暴力求是否有交点需要加break,否则会T;

#include<iostream>
#include<cstdio> 
using namespace std;
struct point 
{
	double x,y;
};
struct vector
{
	point start,end;
};
point p;
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);
}
int Across(vector v1,vector 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;
} 
vector line[222222];
int a[111111];
int main()
{
	int n;
	while(~scanf("%d",&n)&&n)
	{
		for(int i=0;i<n;i++)
		{
		scanf("%lf %lf %lf %lf",&line[i].start.x,&line[i].start.y,&line[i].end.x,&line[i].end.y);
		}
		int flag=0,k=0;
		for(int i=0;i<n;i++)
		{
			flag=0;
			for(int j=i+1;j<n;j++)
			{
				
				if(Across(line[i],line[j]))
				{
					flag=1;
					break;
				}
			}
			if(flag==0)
			a[k++]=i;
		}
		printf("Top sticks:");
		for(int i=0;i<k-1;i++)
		printf(" %d,",a[i]+1);
		printf(" %d.\n",a[k-1]+1);
		
	}
}

你可能感兴趣的:(poj 2653 Pick-up sticks)