经典题目PickUpSticks线段交,STL链表条件判断写法

K - Pick-up Sticks
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

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

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

Output for sample input

Top sticks: 2, 4, 5.
Top sticks: 1, 2, 3.

                        #define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x){return fabs(x) < EPS ? 0 :(x < 0 ? -1 : 1);}
#define N 100005
struct Point;
typedef Point Vec;
struct Point
{
    double x, y;
    Point () {}
    Point(double a, double b)
    {
        x = a;
        y = b;
    }
    Vec operator + (const Vec &b)//点加法
    {
        return Vec(x + b.x, y + b.y);
    }
    Vec operator - (const Vec &b)//点减法
    {
        return Vec(x - b.x, y - b.y);
    }
    Vec operator * (const double &p)//点与常数相乘
    {
        return Vec(x * p, y * p);
    }
    Vec operator / (const double &p)//点除以常数
    {
        return Vec(x / p, y / p);
    }
    bool operator < (const Point &b)//平面直角坐标系中左下方的为小
    {
        return x < b.x || (sgn(x - b.x) == 0 && y < b.y);
    }
    bool operator == (const Point &b)//点相等判断
    {
        return sgn(x - b.x) == 0 && sgn(y - b.y) == 0;
    }
};
inline double dotDet(Vec a, Vec b)//点乘
{
    return a.x * b.x + a.y * b.y;
}
inline double crossDet(Vec a, Vec b)//叉乘
{
    return a.x * b.y - a.y * b.x;
}
inline bool onSeg(Point x, Point a, Point b)//判断点在线段ab上,加上||x==a||x==b在端点也算
{
    return sgn(crossDet(a - x, b - x)) == 0 && sgn(dotDet(a - x, b - x)) < 0 || x == a || x == b;
}
struct Line
{
    Point s, t;//保存线上两点
    int NO;//保存编号
    Line() {}
    Line(Point s, Point t) :s(s), t(t) {}
};
typedef Line Seg;//重定义线段类
// 0 : not intersect
// 1 : proper intersect
// 2 : improper intersect
int segIntersect(Point a, Point c, Point b, Point d)//线段相交判断,返回2是一条线段一端在另一线段上
{
    Vec v1 = b - a, v2 = c - b, v3 = d - c, v4 = a - d;
    int a_bc = sgn(crossDet(v1, v2));
    int b_cd = sgn(crossDet(v2, v3));
    int c_da = sgn(crossDet(v3, v4));
    int d_ab = sgn(crossDet(v4, v1));
    if (a_bc * c_da > 0 && b_cd * d_ab > 0) return 1;
    if (onSeg(b, a, c) && c_da) return 2;
    if (onSeg(c, b, d) && d_ab) return 2;
    if (onSeg(d, c, a) && a_bc) return 2;
    if (onSeg(a, d, b) && b_cd) return 2;
    return 0;
}
inline int segIntersect(Seg a, Seg b)//同上
{
    return segIntersect(a.s, a.t, b.s, b.t);
}
Seg s[N];
Seg S;
template <class T>
class is_itersect: public std::unary_function<T, bool>
{
public:
    bool operator()(T &val)
    {
        if (segIntersect(val, S) == 0)
            return 0;
        else
        {
            return 1;
        }
    }
};
int main()
{    
    #ifdef DeBUGs
        freopen("C:\\Users\\Sky\\Desktop\\1.in","r",stdin);
    #endif
    int n;
    while(scanf("%d", &n),n)
    {
        list<Seg>L;
        list<Seg>::iterator it;
        for(int i=0;i<n;i++)
        {
            scanf("%lf%lf%lf%lf", &s[i].s.x,&s[i].s.y,&s[i].t.x,&s[i].t.y);
            s[i].NO=i+1;
            S=s[i];
            L.remove_if(is_itersect<Seg>());//高级玩法,STL链表条件判断
            L.push_back(S);
        }
        printf("Top sticks:");
        it = L.begin();
        printf(" %d", (*it).NO);
        for (it++; it != L.end(); it++)
        {
            printf(", %d", (*it).NO);
        }
        printf(".\n");
    }
    
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值