[POJ2398]Toy Storage(计算几何+二分)

题目描述

传送门

题解

同POJ2318

代码

#include
#include
#include
#include
#include
using namespace std;
#define N 10005

const double eps=1e-9;
int dcmp(double x)
{
    if (x<=eps&&x>=-eps) return 0;
    return (x>0)?1:-1;
}
struct Vector
{
    double x,y;
    Vector(double X=0,double Y=0)
    {
        x=X,y=Y;
    }
};
typedef Vector Point;
struct Line
{
    Point p;
    Vector v;
    Line(Point P=Point(0,0),Vector V=Vector(0,0))
    {
        p=P,v=V;
    }
    bool operator < (Line A) const
    {
        return p.xoperator - (Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y);}

int n,m;
double a,b,c,d,ui,li,x,y;
Line l[N];
int cnt[N],ans[N];

void clear()
{
    memset(cnt,0,sizeof(cnt));
    memset(ans,0,sizeof(ans));
}
int cmp(Line a,Line b)
{
    return adouble Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}
bool check(int mid,Point pt)
{
    Point st=l[mid].p;
    Vector c=Vector(pt.x-st.x,pt.y-st.y);
    double t=Cross(l[mid].v,c);
    if (dcmp(t)>0) return true;
    else return false;
}
int find(Point pt)
{
    int l=1,r=n+1,mid,ans=0;
    while (l<=r)
    {
        mid=(l+r)>>1;
        if (check(mid,pt)) ans=mid,r=mid-1;
        else l=mid+1;
    }
    return ans;
}

int main()
{
    while (~scanf("%d",&n))
    {
        if (!n) break;
        puts("Box");
        clear();
        scanf("%d",&m);
        scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
        for (int i=1;i<=n;++i)
        {
            scanf("%lf%lf",&ui,&li);
            l[i]=Line(Point(li,d),Vector(ui-li,b-d));
        }
        l[n+1]=Line(Point(c,d),Vector(0,b-d));
        sort(l+1,l+n+2);
        for (int i=1;i<=m;++i)
        {
            scanf("%lf%lf",&x,&y);
            int loc=find(Point(x,y));
            ++cnt[loc];
        }
        for (int i=1;i<=n+1;++i) ++ans[cnt[i]];
        for (int i=1;i<=m;++i)
            if (ans[i])
                printf("%d: %d\n",i,ans[i]);
    }
}

你可能感兴趣的:(题解,计算几何)