hdu 1147 Pick-up sticks(线段相交+枚举)

Pick-up sticks

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2293    Accepted Submission(s): 842


Problem 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.
 

Source
题目分析:
枚举当亲每条直线,然后枚举之前没有覆盖掉的直线,覆盖掉的直线去掉,没有覆盖的保留,最终保留的就是最后最后结果,用一个数组模拟记录即可,姿势很漂亮
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#define MAX 100007
#define N 1007
#define eps 1e-10

using namespace std;

int n,cc;

struct Point 
{
    double x , y;
    Point ():x(0),y(0){}
    Point ( double a , double b )
        :x(a),y(b){}
};

struct Line
{
    Point u,v;
}l[MAX];

int sig ( double d )
{
    return fabs(d)<eps?0:d<0?-1:1;
}

double cross ( Point& o , Point& a , Point& b )
{
    return (a.x-o.x)*(b.y-o.y) - (b.x-o.x)*(a.y-o.y);
}

bool OnSegment ( Point& a , Point& b , Point& c )
{
    return c.x >= min ( a.x , b.x ) && c.x <= max ( a.x , b.x )
        && c.y >= min ( a.y , b.y ) && c.y <= max ( a.y , b.y );
}

bool segCross ( Point& a , Point& b , Point& c , Point& d )
{
    double s1,s2;
    int d1 , d2 , d3 , d4;
    d1 = sig ( s1 = cross ( a , b , c ) );
    d2 = sig ( s2 = cross ( a , b , d ) );
    d3 = sig ( cross ( c , d , a ));
    d4 = sig ( cross ( c , d , b ));
    if ( (d1^d2) == -2 && (d3^d4) == -2 ) return 1;
    else if ( d1 == 0 && OnSegment ( a , b , c ) ) return 1;
    else if ( d2 == 0 && OnSegment ( a , b , d ) ) return 1;
    else if ( d3 == 0 && OnSegment ( c , d , a ) ) return 1;
    else if ( d4 == 0 && OnSegment ( c , d , a ) ) return 1;
    return 0;
}

int single[N];

int main ( )
{
    while ( ~scanf ( "%d" , &n ) , n )
    {
        for ( int i = 1 ; i <= n ; i++ )
            scanf ( "%lf%lf%lf%lf" , &l[i].u.x , &l[i].u.y , &l[i].v.x , &l[i].v.y );
        int p ,q;
        p = 0;
        for ( int i = 1 ; i <= n ; i++ )
        {
            q = 0;
            for ( int j = 0 ; j < p ; j++ )
                if ( !segCross ( l[i].u , l[i].v , l[single[j]].u , l[single[j]].v ) )
                    single[q++] = single[j];
            single[q++] = i;
            p = q;
        }
        printf ( "Top sticks: %d" , single[0] );
        for ( int i = 1 ; i < p ; i++ )
            printf ( ", %d" , single[i] );
        puts ( "." );
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值