贪心,二分,半平面交(丛林警戒队,LA 4992)

21 篇文章 0 订阅
18 篇文章 0 订阅

RE的6种可能

1、数组开小了

2、数组越界了

3、除0了

4、递归爆栈了

5、指针

6、申请空间过多了。


还是感觉自己有问题,码得太多,想的太少,实践得更少。

自己先跟自己玩玩攻防游戏,多玩几盘,就知道正确的防守方法了,然后思路与代码就是分分钟的事情。

先抛开代码与算法不管,然后搞出一种手算的解决方案(像玩单机游戏那样去不断尝试),最后再想办法用代码去实现。


代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 50010;

struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
};

typedef Point Vector;

const double eps = 1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps) return 0;
    else return x<0?-1:1;
}

Point ReadPoint()
{
    double x,y;
    scanf("%lf %lf",&x,&y);
    return Point(x,y);
}

Point operator + (Point A,Vector B)
{
    return Point(A.x+B.x,A.y+B.y);
}

Vector operator - (Point A,Point B)
{
    return Vector(A.x-B.x,A.y-B.y);
}

Vector operator * (Vector A,double t)
{
    return Vector(A.x*t,A.y*t);
}

double Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}

double angle(Vector V)
{
    return atan2(V.y,V.x);
}

struct Line
{
    Point p;
    Vector v;
    double ang;
    Line(Point p=Point(),Vector v=Vector()):p(p),v(v)
    {
        ang=angle(v);
    }
    bool operator < (const Line& rhs) const
    {
        return ang<rhs.ang;
    }
};

bool OnLeft(Line L,Point P)
{
    return dcmp(Cross(L.v,P-L.p))>0;
}

Point GetLineIntersection(Line A,Line B)
{
    Vector u=B.p-A.p;
    double t=Cross(u,B.v)/Cross(A.v,B.v);
    return A.p+A.v*t;
}

Point p[maxn];
Line q[maxn];

int GetHalfPlaneIntersection(Line* L,int n,Point* poly)
{
    sort(L,L+n);
    int first,last;
    q[first=last=0]=L[0];
    for(int i=1;i<n;i++)
    {
        while(first<last&&!OnLeft(L[i],p[last-1])) last--;
        while(first<last&&!OnLeft(L[i],p[first])) first++;
        q[++last]=L[i];
        if(dcmp(Cross(q[last].v,q[last-1].v))==0)
        {
            last--;
            if(OnLeft(q[last],L[i].p)) q[last]=L[i];
        }
        if(first<last) p[last-1]=GetLineIntersection(q[last-1],q[last]);
    }
    while(first<last&&!OnLeft(q[first],p[last-1])) --last;
    if(last-first<=1) return 0;
    p[last]=GetLineIntersection(q[last],q[first]);
    int m=0;
    for(int i=first;i<=last;++i)
        poly[m++]=p[i];
    return m;
}

int n;
Point P[maxn];
Point poly[maxn];
Line L[maxn];

int main()
{
    while(scanf("%d",&n)==1&&n)
    {
        for(int i=0;i<n;i++)
            P[i]=ReadPoint();
        for(int i=0;i<n/2;i++)
        {
            Point temp=P[i];
            P[i]=P[n-i-1];
            P[n-i-1]=temp;
        }
        int l=1,r=n;
        while(l<r)
        {
            int m=l+(r-l)/2;
            for(int i=0;i<n;i++)
                L[i]=Line(P[i],P[(i+m)%n]-P[i]);
            if(!GetHalfPlaneIntersection(L,n,poly)) r=m;
            else l=m+1;
        }
        printf("%d\n",l-1);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值