poj2653-Pick-up sticks(线段相交问题)

Pick-up sticks
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 9883   Accepted: 3667

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. poj2653-Pick-up sticks(线段相交问题)_第1张图片

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

Waterloo local 2005.09.17

#include
#include
#define MAXN 100005
const double epsi=1e-10;
inline int	sign(const double &x);
inline double sqr(const double &x);
	
struct Point{
	double x,y;

	Point(double _x=0,double _y=0):x(_x),y(_y){
	}
	Point operator +(const Point &op2) const{
	return Point(x+op2.x,y+op2.y);
	}
	
Point operator -(const Point &op2) const{
	return Point(x-op2.x,y-op2.y);
	}
	double operator ^(const Point &op2) const{
	return x*op2.y-y*op2.x;
	}
		double operator *(const Point &op2) const{
	return x*op2.x+y*op2.y;
	}
	Point operator *(const double &d) const{
	return Point(x*d,y*d);
	}
	Point operator /(const double &d) const{
	return Point(x/d,y/d);
	}
	

	bool operator ==(const Point &op2) const{
	return sign(x-op2.x)==0&&sign(y-op2.y)==0;
	}
	
};

inline int	sign(const double &x){
		if(x>epsi) return 1;
		if(x<-epsi) return -1;
		return 0;	
		}
		
inline double mul(const Point &p0,const Point &p1,const Point &p2){//p0p1与p0p2叉积 
	return (p1-p0)^(p2-p0);
}
	inline double sqr(const double &x){
		return x*x;
	}
inline double dis2(const Point &p0,const Point &p1){//p0p1平方 
	return sqr(p0.x-p1.x)+sqr(p0.y-p1.y);
}
	inline double dis(const Point &p0,const Point &p1){//p0p1
	return sqrt(dis2(p0,p1));
}
inline int cross(const Point &p1,const Point &p2,const Point &p3,const Point &p4){//求p1p2,p3p4是否跨立
int sign1=sign(mul(p1,p3,p2));//求p1p3和p1p2叉积,
 int sign2=sign(mul(p1,p4,p2));//求p1p4和p1p2叉积,
 if(sign1==sign2) return 0;//不跨立
 if(sign1==0&&sign2==0) return 2;//p3或p4在p1p2上
 return 1; 
 
}
bool visit[MAXN];
Point p1[MAXN],p2[MAXN];
int main(){
	int n;
	while(scanf("%d",&n),n){
		for(int i=0;i

题目思路:判断前面丢的是否和后面丢的相交,采用直线跨立:
如p1p2 和p3p4判断是否相交,做辅助线p1p3和p1p4
求p1p3和p1p2的叉积还有p1p4和p1p2的叉积。如果符号相同,表明在p3p4在p1p2同一边 ,则我们说线段p1p2跨立直线P3P4
如果符号不同,则表明在两边,不跨立
如果其中一个为0则P3 或P4 在直线P1P2 上,也算跨立。
poj2653-Pick-up sticks(线段相交问题)_第2张图片
所以只要互相判断是否跨立就行了。

你可能感兴趣的:(acm)