poj3335 Rotating Scoreboard

题目描述:

vjudge

POJ

题解:

半平面交判核的存在性。

重点在于一个点的核也算核

这样的话普通的求多边形的版本就要加一个特判。

就是把剩下的一个节点暴力带回所有直线重判,这时判叉积是否$\leq 0$,而不是$<0$。

好人一生平安

代码:

#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 150;
const double eps = 1e-8;
int dcmp(double x)
{
    if(fabs(x)<=eps)return 0;
    return x>0?1:-1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double x,double y):x(x),y(y){}
    Point operator + (const Point&a)const{return Point(x+a.x,y+a.y);}
    Point operator - (const Point&a)const{return Point(x-a.x,y-a.y);}
    Point operator * (const double&a)const{return Point(x*a,y*a);}
    double operator ^ (const Point&a)const{return x*a.y-y*a.x;}
};
typedef Point Vector;
typedef vector<Point> Pol;
double ang(const Vector&v){return atan2(v.x,v.y);}
struct Line
{
    Point p;
    Vector v;
    Line(){}
    Line(Point p,Vector v):p(p),v(v){}
    bool operator < (const Line&a)const
    {
        return ang(v)<ang(a.v);
    }
};
bool Onleft(Line a,Point p)
{
    return dcmp((a.v^(p-a.p)))>0;
}
bool onleft(Line a,Point p)
{
    return dcmp((a.v^(p-a.p)))>=0;
}
Point L_L(Line a,Line b)
{
    double t = ((b.p-a.p)^b.v)/(a.v^b.v);
    return a.p+a.v*t;
}
int T,n;
Point p[N],tp[N];
Line s[N],ts[N];
int bpmj()
{
    memset(ts,0,sizeof(ts));memset(tp,0,sizeof(tp));
    sort(s+1,s+1+n);
    int hd,tl;
    ts[hd=tl=1]=s[1];
    for(int i=2;i<=n;i++)
    {
        while(hd<tl&&!Onleft(s[i],tp[tl-1]))tl--;
        while(hd<tl&&!Onleft(s[i],tp[hd]))hd++;
        ts[++tl] = s[i];
        if(!dcmp(ts[tl].v^ts[tl-1].v))
        {
            tl--;
            if(Onleft(ts[tl],s[i].p))ts[tl]=s[i];
        }
        if(hd<tl)tp[tl-1] = L_L(ts[tl-1],ts[tl]);
    }
    while(hd<tl&&!Onleft(ts[hd],tp[tl-1]))tl--;
    if(hd==tl)
    {
        Point now = tp[hd];
        for(int i=1;i<=n;i++)
            if(!onleft(s[i],now))
                return 0;
    }
    return 1;
}
void build1()
{
    for(int i=1;i<n;i++)s[i]=Line(p[i],p[i+1]-p[i]);
    s[n] = Line(p[n],p[1]-p[n]);
}
void build2()
{
    for(int i=2,j=n;i<j;i++,j--)swap(p[i],p[j]);
    for(int i=1;i<n;i++)s[i]=Line(p[i],p[i+1]-p[i]);
    s[n] = Line(p[n],p[1]-p[n]);
}
void work()
{
    scanf("%d",&n);int ans = 0;
    for(int i=1;i<=n;i++)scanf("%lf%lf",&p[i].x,&p[i].y);
    build1();
    ans|=bpmj();
    build2();
    ans|=bpmj();
    if(ans)puts("YES");
    else puts("NO");
}
int main()
{
//    freopen("tt.in","r",stdin);
    scanf("%d",&T);
    while(T--)work();
    return 0;
}
View Code

 

posted @ 2019-06-06 11:40  LiGuanlin  阅读(219)  评论(0编辑  收藏  举报