POJ 2653 Pick-up sticks(判断线段相交,有一点点小优化)

POJ 2653 Pick-up sticks(判断线段相交,有一点点小优化)

思路:就是判断线段与线段之间是否相交。

因为数据量很大,要一边遍历一边排除掉已经被压着的数据,这样的话才能卡着时间过。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include <cstdio>
using namespace std;
#define ll long long
struct Point
{
    double x,y;
    Point(double x= 0, double y = 0):x(x),y(y){}
};
typedef Point Vector;
const int INF = 0x3f3f;
Vector operator-(Point A,Point B)
{
    return Vector(A.x-B.x,A.y-B.y);
}
Vector operator *(Vector A, double p)
{
    return Vector(A.x*p, A.y*p);
}
Vector operator +(Vector A, Vector B){
    return Vector(A.x+B.x, A.y+B.y);
}
Vector operator /(Vector A, double p){return Vector(A.x/p, A.y/p);}
double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y;}
double Length(Vector A){ return sqrt(Dot(A, A));}
double Cross(Vector A, Vector B){return A.x*B.y-A.y*B.x;}
double dis(Point a,Point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
const double eps = 1e-6;
int sgn(double x){
    if(fabs(x) < eps)
        return 0;
    if(x < 0)
        return -1;
    return 1;
}
struct Line{
    Point v, p;
    Line(){}
    Line(Point v, Point p):v(v), p(p){}
    Point point(double t){
        return v+(p-v)*t;//·µ»ØµãP = v + (p - v)*t
    }
}line[105000];
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
    double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
    double c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
    return (sgn(c1)*sgn(c2) < 0 && sgn(c3)*sgn(c4) < 0);
}


queue<int>q;
queue<int>temp;

int main()
{
    int n;
    while(scanf("%d",&n) !=EOF)
    {
        if(n == 0)
            return 0;
        for(int i = 1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&line[i].v.x, &line[i].v.y, &line[i].p.x, &line[i].p.y);
        }
        q.push(1);
        for(int i = 2;i<=n;i++)
        {
            while(!q.empty())
            {
                int id  = q.front();
                q.pop();
                if(!SegmentProperIntersection(line[id].v, line[id].p, line[i].v,line[i].p))
                {
                    temp.push(id);
                }
            }
            while(!temp.empty())
            {
                q.push(temp.front());
                temp.pop();
            }
            q.push(i);
        }
        printf("Top sticks:");
        int cnt = 0;
        while(!q.empty())
        {
            if(cnt)
                printf(",");
            cnt++;
            printf(" %d",q.front());
            q.pop();
        }
        printf(".\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值