Pick-up sticks(线段相交判断+剪枝)

Link:http://poj.org/problem?id=2653


Pick-up sticks
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 11050 Accepted: 4120

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



AC code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define EPS 1e-8
using namespace std;
#define MIN(x,y) (x < y ? x : y)
#define MAX(x,y) (x > y ? x : y) 
typedef struct {
    double x,y;
} Point;
struct line{
	Point s;
	Point e;
}seg[100010];
bool deleted[100010];
int ans[100010];
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 Acoross(struct line v1,struct line v2)
{
    if(max(v1.s.x,v1.e.x)>=min(v2.s.x,v2.e.x)&&
       max(v2.s.x,v2.e.x)>=min(v1.s.x,v1.e.x)&&
       max(v1.s.y,v1.e.y)>=min(v2.s.y,v2.e.y)&&
       multi(v2.s,v1.e,v1.s)*multi(v1.e,v2.e,v1.s)>=0&&
       multi(v1.s,v2.e,v2.s)*multi(v2.e,v1.e,v2.s)>=0)
        return 1;//相交
    return 0;//不相交
}
int main()
{
   //freopen("D:\in.txt","r",stdin);
    int n,i,j,k,fg,t,cnt;
	while(scanf("%d",&n)!=EOF)
	{
		if(n==0)
			break;
		for(i=1;i<=n;i++)
		{
			scanf("%lf%lf%lf%lf",&seg[i].s.x,&seg[i].s.y,&seg[i].e.x,&seg[i].e.y);
		}
		memset(deleted,false,sizeof(deleted));
		for(i=1;i<n;i++)
		{
			for(j=i+1;j<=n;j++)
			{
				if(Acoross(seg[i],seg[j]))
				{
					deleted[i]=true;
					//printf("%d %d\n",i,j);
					break;//剪枝,不然TLE!!! 
				}
			}
		}
		printf("Top sticks:");
		cnt=0;
		for(i=1;i<=n;i++)
		{
			if(!deleted[i])
			{
				ans[++cnt]=i;
			}	
		}
		for(i=1;i<cnt;i++)
			printf(" %d,",ans[i]);
		printf(" %d.\n",ans[cnt]);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值