Toy Storage ZOJ - 1996

解释一下输出:格子里有2个玩具的格子有5个

二分查找,用map输出

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <map>

using namespace std;
const int maxn=1111;
const int eps=1e-8;

int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    if(x<0)
        return -1;
    else
        return 1;
}
struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x=_x;
        y=_y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x-b.x,y-b.y);
    }
    double operator ^(const Point &b)const
    {
        return x*b.y-y*b.x;
    }
    double operator *(const Point &b)const
    {
        return x*b.x+y*b.y;
    }
};

struct Line
{
    Point s,e;
    Line(){}
    Line(Point _s,Point _e)
    {
        s=_s;
        e=_e;
    }
};
Point p[maxn];
int num[maxn];
Line line[maxn];
int ans[maxn];
bool cmp(Line a,Line b)
{
    return a.s.x<b.s.x;
}
map <int,int> mp;
int main()
{
    int n,m,x1,y1,x2,y2;
    while(1)
    {
        memset(num,0,sizeof(num));
        cin>>n;
        if(n==0)
            break;
        cin>>m>>x1>>y1>>x2>>y2;
        int cont=0;
        for(int i=0;i<n;i++)
        {
           double x1,x2;
           cin>>x1>>x2;
           line[i]=Line(Point(x1,y1),Point(x2,y2));
        }
        line[n]=Line(Point(x1,y1),Point(0,0));
        n++;
        line[n]=Line(Point(x2,y2),Point(x2,y1));
        n++;
        sort(line,line+n,cmp);
        for(int i=0;i<m;i++)
        {
            double x1,y1;
            cin>>x1>>y1;
            int l,r;
            l=0;
            r=n-1;
            Point p1=Point(x1,y1);
            int mid;
            while(l<=r)
            {
                mid=(l+r)/2;
                int ans=sgn((line[mid].s-p1)^(line[mid].e-p1));
                if(ans==-1)
                {
                    r=mid-1;
                }
                else if(ans==1)
                    l=mid+1;
            }
            num[(l+r)/2]++;
        }
        sort(num,num+maxn);
        cout<<"Box"<<endl;
        mp.clear();
        for(int i=0;i<maxn;i++)
        {
            if(num[i]!=0)
            {
                mp[num[i]]++;
            }
        }
        map <int,int>::iterator it;
        for(it=mp.begin();it!=mp.end();it++)
        {
            printf("%d: %d\n",it->first,it->second);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/leekerian/article/details/81351147
ZOJ