POJ-2653

这题就是大模拟,用队列来保证这些线段的先后顺序,再用一个队列来辅助存储——就是类似于滚动数组那样,再开两个int类型的队列存编号,原理同上。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <climits>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <vector>
#include <utility>
using namespace std;
struct P
{
    double x, y;
    P() {}
    P(double x, double y) : x(x), y(y) {}
    P operator +(const P &p)
    {
        return P(x + p.x, y + p.y);
    }
    P operator -(const P &p)
    {
        return P(x - p.x, y - p.y);
    }
    P operator *(const double &d)
    {
        return P(x * d, y * d);
    }
    P operator /(const double &d)
    {
        return P(x / d, y / d);
    }
    bool operator ==(const P &p)
    {
        return x == p.x && y == p.y;
    }
    P operator =(const P &p)
    {
        x = p.x, y = p.y;
        return *this;
    }
    double det(const P &p)
    {
        return x * p.y - y * p.x;
    }
    double dot(const P &p)
    {
        return x * p.x + y * p.y;
    }
    void show()
    {
        cout << x << " " << y << " ";
    }
};
queue< pair<P, P> > seg_que0, seg_que1;
queue<int> no_que0, no_que1;

inline void init()
{
    while(seg_que0.size()) seg_que0.pop();
    while(seg_que1.size()) seg_que1.pop();
    while(no_que0.size()) no_que0.pop();
    while(no_que1.size()) no_que1.pop();
}

inline bool on_point(P p1, P p2, P q1, P q2)
{
    return p1 == q2 || p1 == q1 || p2 == q1 || p2 == q2;
}

inline bool on_seg(P p1, P p2, P q)
{
    return (q - p1).det(p2 - q) == 0 && (q - p1).dot(p2 - q) >= 0;
}

inline bool on_inster(P p1, P p2, P q1, P q2)
{
    return ((p1 - q2).det(q1 - q2)) * ((q1 - q2).det(p2 - q2)) > 0 && ((q1 - p2).det(p1 - p2)) * ((p1 - p2).det(q2 - p2)) > 0;
}

int main()
{
    int n, i, j, k;
    while(scanf("%d", &n) != EOF && n != 0)
    {
        init();
        P start, fine;
        for(i = 0; i < n; ++i)
        {
            if(i == 0)
            {
                scanf("%lf %lf %lf %lf", &start.x, &start.y, &fine.x, &fine.y);
                seg_que0.push(pair<P, P>(start, fine));
                no_que0.push(i);
            }
            else
            {
                scanf("%lf %lf %lf %lf", &start.x, &start.y, &fine.x, &fine.y);
                if(i % 2 == 0)
                {
                    while(seg_que1.size())
                    {
                        pair<P, P> tmp = seg_que1.front();
                        seg_que1.pop();
                        int pos = no_que1.front();
                        no_que1.pop();
                        if(on_point(start, fine, tmp.first, tmp.second)) continue;
                        else if(on_seg(start, fine, tmp.first) || on_seg(start, fine, tmp.second) ||
                                on_seg(tmp.first, tmp.second, start) || on_seg(tmp.first, tmp.second, fine)) continue;
                        else if(on_inster(start, fine, tmp.first, tmp.second)) continue;
                        else
                        {
                            seg_que0.push(tmp);
                            no_que0.push(pos);
                        }
                    }
                    seg_que0.push(pair<P, P>(start, fine));
                    no_que0.push(i);
                }
                else
                {
                    while(seg_que0.size())
                    {
                        pair<P, P> tmp = seg_que0.front();
                        seg_que0.pop();
                        int pos = no_que0.front();
                        no_que0.pop();
                        if(on_point(start, fine, tmp.first, tmp.second)) continue;
                        else if(on_seg(start, fine, tmp.first) || on_seg(start, fine, tmp.second) ||
                                on_seg(tmp.first, tmp.second, start) || on_seg(tmp.first, tmp.second, fine)) continue;
                        else if(on_inster(start, fine, tmp.first, tmp.second)) continue;
                        else
                        {
                            seg_que1.push(tmp);
                            no_que1.push(pos);
                        }
                    }
                    seg_que1.push(pair<P, P>(start, fine));
                    no_que1.push(i);
                }
            }
        }
        printf("Top sticks: ");
        if(n % 2 == 0)
        {
            k = 0;
            while(no_que1.size() != 0)
            {
                if(k != 0) printf(", ");
                printf("%d", no_que1.front() + 1);
                no_que1.pop();
                k += 1;
            }
        }
        else
        {
            k = 0;
            while(no_que0.size() != 0)
            {
                if(k != 0) printf(", ");
                printf("%d", no_que0.front() + 1);
                no_que0.pop();
                k += 1;
            }
        }
        printf(".");
        puts("");
    }
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值