poj 2653 Pick-up sticks(计算几何)

Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 11836 Accepted: 4475

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.

Source

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:给出平面内的N条线段,线段以读入关系为上下关系,判段一个线段是否被覆盖的标准是他与他后面加入的线段是否存在交点,如果存在交点则该线段不合法,求出最后所有的合法线段。

判断两个线段相交:判断是否相互跨立。

如果两线段相交,则两线段必然相互跨立对方。若P1P2跨立Q1Q2 ,则矢量 ( P1 - Q1 ) 和( P2 - Q1 )位于矢量( Q2 - Q1 ) 的两侧,即( P1 - Q1 ) × ( Q2 - Q1 ) * ( P2 - Q1 ) × ( Q2 - Q1 ) < 0。上式可改写成( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) > 0。当 ( P1 - Q1 ) × ( Q2 - Q1 ) = 0 时,说明 ( P1 - Q1 ) 和 ( Q2 - Q1 )共线,但是因为已经通过快速排斥试验,所以 P1 一定在线段 Q1Q2上;同理,( Q2 - Q1 ) ×(P2 - Q1 ) = 0 说明 P2 一定在线段 Q1Q2上。所以判断P1P2跨立Q1Q2的依据是:( P1 - Q1 ) × ( Q2 - Q1 ) * ( Q2 - Q1 ) × ( P2 - Q1 ) >= 0。同理判断Q1Q2跨立P1P2的依据是:( Q1 - P1 ) × ( P2 - P1 ) * ( P2 - P1 ) × ( Q2 - P1 ) >= 0。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define N 100003
#define esp 1e-6
using namespace std;
int n,m,st[N],top,vis[N];
struct point
{
	double x,y;
	point(){
		x=y=0;
	};
	point operator -(const point &a)
	{
		point t; t.x=x-a.x; t.y=y-a.y;
		return t;
	}
};
struct data
{
	point a,b;
}p[N];
struct vector
{
	double x,y;
	vector(){
		x=y=0;
	};
	vector operator =(const point &a)
	{
		this->x=a.x; this->y=a.y;
		return *this;
	}
	double operator *(const vector &a)
	{
		return x*a.y-y*a.x;
	}
}len[N];
bool pd(int i,int j)
{
    vector t1,t2,t3,t4;
    t1=p[i].a-p[j].a;  t2=p[i].b-p[j].a;
    t3=p[j].a-p[i].a;  t4=p[j].b-p[i].a;
	return (t1*len[j])*(len[j]*t2)>=0&&(t3*len[i])*(len[i]*t4)>=0;
}
int main()
{
	while (scanf("%d",&n)!=EOF)
	{
		if (!n) break; 
		for (int i=1;i<=n;i++)
		 {
		 	point a,b; 
		 	scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);
		 	p[i].a.x=a.x; p[i].a.y=a.y;
		 	p[i].b.x=b.x; p[i].b.y=b.y;
		 	len[i]=b-a;
		 }
		memset(st,0,sizeof(st)); top=0;
		top++; st[top]=1;
		for (int i=2;i<=n;i++)
		 {
		 	for (int j=1;j<=top;j++)
		 	 if (pd(i,st[j]))
		 	  vis[j]=1;
		 	int t=top; top=0;
		 	for (int j=1;j<=t;j++)
		 	 if (!vis[j])  
			    st[++top]=st[j];
		 	st[++top]=i;
		 	for (int j=1;j<=top;j++) vis[j]=0;
		 }
		printf("Top sticks:");
		for (int i=1;i<=top-1;i++)
		 printf(" %d,",st[i]);
		printf(" %d.\n",st[top]);
	}
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值