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);
		
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章