hdu_1147 Pick_up sticks

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.

分析:

      解题思路比较简单——每放一根棍子,都判断一下它与它前面的且在顶端的棍子是否相交,相交的话则将相应的棍子从解空间中除去(当前这根暂时是在解空间中的)

      但是,如果直接用数组做会超时,然后用链表做(刚开始自己写了个链表,但是写坏掉了,哎,基础啊……),于是最后还是用了list

      除数据结构外,最主要的算法就是判断两直线相交了,直接用了模板


代码:

//hdu 1147
#include <iostream>
#include <cstdio>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <list>
using namespace std;
const double eps=1e-8;

int sgn(double x)
{
    if(fabs(x)<eps) return 0;
    else return x<0 ? -1:1;
}

struct point
{
    double x,y;
    point(){}
    point(double _x,double _y)  //带参构造函数
    {
                x = _x;y = _y;
    }
    point operator -(const point &b) const
    {
          return point(x-b.x,y-b.y);
    }

    double operator ^(const point &b) const
    {
          return x*b.y-y*b.x;
    }
};

struct line
{
    point s,e;
};

struct node{
   line t;
   int no;
};

bool inter(line l1,line l2)
{
    return
    max(l1.s.x,l1.e.x) >=min(l2.s.x,l2.e.x) &&
    max(l2.s.x,l2.e.x) >=min(l1.s.x,l1.e.x) &&
    max(l1.s.y,l1.e.y) >=min(l2.s.y,l2.e.y) &&
    max(l2.s.y,l2.e.y) >=min(l1.s.y,l1.e.y) &&
    sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e))<=0 &&
    sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e))<=0 ;
}

int main()
{
    freopen("in.txt","r",stdin);
    int n;
    int len;
    node cur;
    list<node> stick;
    list<node>::iterator p;

    while(scanf("%d",&n),n){

        for(int i=1;i<=n;i++){
            scanf("%lf%lf%lf%lf",&cur.t.s.x,&cur.t.s.y,&cur.t.e.x,&cur.t.e.y);
            cur.no=i;
            stick.push_back(cur);

            if(i>1){
                p=stick.begin();
                len=stick.size();
                for(int i=1;i<len;i++){
                    if(inter( (*p).t, cur.t))
                       p=stick.erase(p);
                    else
                       p++;
                }
            }

        }
        printf("Top sticks: ");
        p=stick.begin();
        len=stick.size();
        for(int i=1;i<len;i++){
            printf("%d, ",(*p).no);
            p++;
        }
        printf("%d.\n",(*p).no);
        stick.clear();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值