poj2653-Pick-up sticks

Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 9724 Accepted: 3602

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.

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.

线段求交的几何问题,对于set的erase()方法,g++的编译器好像没有返回值。
代码如下:
#include<iostream>
#include<algorithm>
#include<set>
#include<cstring>
using namespace std;
int N;
struct Seg{//线段结点 
	double x1,y1,x2,y2;
}seg[100005];
struct endPoint{//端点 
	double x,y;
	endPoint(double a,double b):x(a),y(b){}
};
istream& operator >> (istream &IN,Seg &x){
	IN>>x.x1>>x.y1>>x.x2>>x.y2;
	return IN;
}
ostream& operator<<(ostream& OS,endPoint& t){//读入数据(注意使用引用,两个都用引用) 
	OS<<t.x<<" "<<t.y<<" ";
	return OS;
}
typedef endPoint vet;
set<int> st;
set<int>::iterator it;
double crossProduct(endPoint &x,endPoint &y,endPoint &z){//矢量叉积 
	vet v1(y.x-x.x,y.y-x.y);
	vet v2(z.x-x.x,z.y-x.y);
	return v1.x*v2.y-v2.x*v1.y;
}
bool onseg(endPoint& x,endPoint& y,endPoint& z){//点在线段上,点x在线段yz上 
	if(max(y.x,z.x)>x.x&&min(y.x,z.x)<x.x&&max(y.y,z.y)>x.y&&min(y.y,z.y)<x.y)return true;
	return false;
}
bool intersect (int x,int y){//线段求交 
	endPoint p1(seg[x].x1,seg[x].y1);
	endPoint p2(seg[x].x2,seg[x].y2);
	endPoint p3(seg[y].x1,seg[y].y1);
	endPoint p4(seg[y].x2,seg[y].y2);
	double clockWise1=crossProduct(p1,p2,p3);
	double clockWise2=crossProduct(p1,p2,p4);
	double clockWise3=crossProduct(p3,p4,p1);
	double clockWise4=crossProduct(p3,p4,p2);/*
	cout<<x<<" "<<y<<" "<<p1<<" "<<p2
	<<"********"<<clockWise1<<" "
	<<clockWise2<<" "<<clockWise3<<" "<<clockWise4<<endl;*/
	if(clockWise1*clockWise2<0&&clockWise3*clockWise4<0){
		return true;
	}
	else if(clockWise1==0&&onseg(p3,p1,p2))return true;
	else if(clockWise2==0&&onseg(p4,p1,p2))return true;
	else if(clockWise3==0&&onseg(p1,p3,p4))return true;
	else if(clockWise4==0&&onseg(p2,p3,p4))return true;
	return false;
}
int main(){
	int i,j,k,m,n;
	ios::sync_with_stdio(false);
	while(cin>>N&&N){
		cout<<"Top sticks:";
		st.clear();
		for(i=1;i<=N;i++){
			cin>>seg[i];
		}
		for(i=1;i<=N;i++){
			for(it=st.begin();it!=st.end();){
				if(intersect(i,*it)) {
					it=st.erase(it);//本句在g++下无法通过编译,c++可以AC(似乎是因为g++下erase方法无返回值) 
//					it=st.begin();
				}
				else {
					it++;
				}
			}
			st.insert(i);
		}
		it=st.begin();
		cout<<" "<<*it;
		for(it++;it!=st.end();it++){
			cout<<", "<<*it;
		}
		cout<<"."<<endl;
	}
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值