Fork me on GitHub

POJ 2653 Pick-up sticks --队列,几何

题意: 按顺序扔木棒,求出最上层的木棒是哪些。

解法: 由于最上层的木棒不超过1000个,所以用一个队列存储最上层的木棒,每次扔出一个木棒后,都与队列中的木棒一一判断,看此木棒是否在某一最上层的木棒的上面,即判线段是否相交(两次跨立实验),如果相交,则将那个被压的木棒抛出队列,最后再加入扔的这个木棒到队列中。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#define eps 1e-8
using namespace std;
#define N 100017

struct Point{
    double x,y;
    Point(double x=0, double y=0):x(x),y(y) {}
    void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
struct Circle{
    Point c;
    double r;
    Circle(){}
    Circle(Point c,double r):c(c),r(r) {}
    Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); }
    void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); }
};
struct Line{
    Point p;
    Vector v;
    double ang;
    Line(){}
    Line(Point p, Vector v):p(p),v(v) { ang = atan2(v.y,v.x); }
    Point point(double t) { return Point(p.x + t*v.x, p.y + t*v.y); }
    bool operator < (const Line &L)const { return ang < L.ang; }
};
int dcmp(double x) {
    if(x < -eps) return -1;
    if(x > eps) return 1;
    return 0;
}
template <class T> T sqr(T x) { return x * x;}
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
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); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; }
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 Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
Vector VectorUnit(Vector x){ return x / Length(x);}
Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);}
double angle(Vector v) { return atan2(v.y, v.x); }

bool OnSegment(Point P, Point A, Point B) {
    return dcmp(Cross(A-P,B-P)) == 0 && dcmp(Dot(A-P,B-P)) < 0;
}
double DistanceToSeg(Point P, Point A, Point B)
{
    if(A == B) return Length(P-A);
    Vector v1 = B-A, v2 = P-A, v3 = P-B;
    if(dcmp(Dot(v1, v2)) < 0) return Length(v2);
    if(dcmp(Dot(v1, v3)) > 0) return Length(v3);
    return fabs(Cross(v1, v2)) / Length(v1);
}
double DistanceToLine(Point P, Point A, Point B){
    Vector v1 = B-A, v2 = P-A;
    return fabs(Cross(v1,v2)) / Length(v1);
}
Point GetLineIntersection(Line A, Line B){
    Vector u = A.p - B.p;
    double t = Cross(B.v, u) / Cross(A.v, B.v);
    return A.p + A.v*t;
}
bool SegmentIntersection(Point A,Point B,Point C,Point D) {
    if(dcmp(Cross(C-A,B-A)*Cross(D-A,B-A)) <= 0 && dcmp(Cross(A-C,D-C)*Cross(B-C,D-C)) <= 0) return true;
    return false;
}
//data segment
struct node{
    Point P[2];
}p[N];
vector<int> G;
//data ends

int main()
{
    int n,i,j;
    while(scanf("%d",&n)!=EOF && n)
    {
        queue<int> q;
        G.clear();
        for(i=1;i<=n;i++)
            p[i].P[0].input(), p[i].P[1].input();
        for(i=1;i<=n;i++)
        {
            Point A = p[i].P[0], B = p[i].P[1];
            int sz = q.size();
            while(sz--)
            {
                int now = q.front();
                q.pop();
                if(!SegmentIntersection(A,B,p[now].P[0],p[now].P[1]))
                    q.push(now);
            }
            q.push(i);
        }
        while(!q.empty())
            G.push_back(q.front()), q.pop();
        sort(G.begin(),G.end());
        printf("Top sticks:");
        for(i=0;i<G.size()-1;i++)
            printf(" %d,",G[i]);
        printf(" %d.\n",G[i]);
    }
    return 0;
}
View Code

 

posted @ 2014-11-19 10:32  whatbeg  阅读(207)  评论(0编辑  收藏  举报