您的位置:首页 > 其它

POJ 2653 Pick-up sticks

2014-05-05 17:31 253 查看
链接:http://poj.org/problem?id=2653

题目:

Pick-up sticks

Time Limit: 3000MSMemory Limit: 65536K
Total Submissions: 9127Accepted: 3367
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.


解题思路:

按从前往后的递增顺序记录下每条线段,从前往后遍历(对于线段来讲,就是从下往上遍历),如果没有线段与其相交(即这条线段上面没有覆盖其他线段),那么这条线段就是最上面的线段。算法时间复杂度为O(n2)。这里最关键的地方就是判断两条线段是否相交,也就是要判断两条线段是否互相跨立,对于这个问题,我们可以利用叉积的性质来解,线段AB,线段CD,如果(向量AB^(叉积)向量AC) *( 向量AB^(叉积)向量AD) < 0, 表明C、D两点对线段AB跨立,= 0,表明C、D中有一点在AB所在的直线上, > 0,表明C、D两点在线段AB同侧。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAXN = 100010;
struct Segment
{
	double sx, sy, ex, ey;
};
Segment Seg[MAXN];

double fun(double x1, double y1, double x2, double y2)
{
	return x1 * y2 - x2 * y1;
}

bool isIntersect(Segment a, Segment b)
{
	if(fun(a.ex - a.sx, a.ey - a.sy, b.sx - a.sx, b.sy - a.sy) * 
	   fun(a.ex - a.sx, a.ey - a.sy, b.ex - a.sx, b.ey - a.sy) <= 0 &&
	   fun(b.ex - b.sx, b.ey - b.sy, a.sx - b.sx, a.sy - b.sy) * 
	   fun(b.ex - b.sx, b.ey - b.sy, a.ex - b.sx, a.ey - b.sy) <= 0)
	   return true;
	return false;
}

int main()
{
	int n;
	
	while(~scanf("%d", &n) && n)
	{
		memset(Seg, 0, sizeof(Seg));
		for(int i = 0; i < n; i++)
		{
			scanf("%lf%lf%lf%lf", &Seg[i].sx, 
			&Seg[i].sy, &Seg[i].ex, &Seg[i].ey);
		}
		
		printf("Top sticks:");
		bool first = true;
		for(int i = 0; i < n; i++)
		{
			bool flag = true;
			for(int j = i + 1; j < n; j++)
			{
				if(isIntersect(Seg[i], Seg[j]))
				{
					flag = false;
					break;
				}
			}
			if(flag)
			{
				if(!first)
				{
					printf(", %d", i + 1);
				}
				else
				{
					printf(" %d", i + 1);
					first = false;
				}
			}
		}
		puts(".");
	}
	
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: