Pick-up sticks POJ - 2653

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.

题目大意:丢一堆木棍找最上面的一个或多个(都玩过那个挑木棍的游戏没);按样例一解释就是  :  从第一根木棍依次丢,找出最上面的木棍。可以发现2,4,5号木棍上面均没有木棍。

直接暴力枚举,判断该木棍之后的有没有其它木棍与他相加即可。

关于判断两线段相交我的另一篇博客有写。https://blog.csdn.net/nothing_227/article/details/80586141

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e6+5;
struct line
{
    double x1,y1,x2,y2;
}spe[maxn];
int vis[1005];
bool solve(line a,line b)
{
	if(((a.x1-b.x1)*(a.y2-b.y1)-(a.x2-b.x1)*(a.y1-b.y1))*((a.x1-b.x2)*(a.y2-b.y2)-(a.x2-b.x2)*(a.y1-b.y2))>0) return false;
	if(((b.x1-a.x1)*(b.y2-a.y1)-(b.x2-a.x1)*(b.y1-a.y1))*((b.x1-a.x2)*(b.y2-a.y2)-(b.x2-a.x2)*(b.y1-a.y2))>0) return false;
	return true;
}
int main()
{
    int n;
    while(scanf("%d",&n)==1&&n)
    {
        for(int i = 1; i <=n;i++)
            scanf("%lf%lf%lf%lf",&spe[i].x1,&spe[i].y1,&spe[i].x2,&spe[i].y2);
        int cnt = 0;
        for(int i = 1; i < n; i++)
        {
            bool judge = true;
            for(int j = i+1; j <=n; j++)
                if(solve(spe[i],spe[j]))//不成立
                {
                    judge = false;
                    break;
                }
            if(judge)
                vis[cnt++] = i;
        }
        printf("Top sticks: ");
        for(int i = 0; i < cnt; i++)
            printf("%d, ",vis[i]);
        printf("%d.\n",n);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Nothing_227/article/details/81199415