poj 2398 Toy Storage(叉积判断点是否在四边形内)

题意:给出矩形的左上和右下的坐标,在矩形中有n个木棒,木棒之间不会相交,然后给出木棒上下端点的横坐标,这些木棒将矩形分成多个区域,接着有m个玩具,给出玩具的坐标。输出在每个区域中玩具的个数。
思路:叉积判断点是否在四边形区域内。和poj 2318一样的题,只是输入n个木棒的坐标时为无序输入,需进行排序处理,输出时输出玩具数大于0的区域。
poj 2318参考:http://blog.csdn.net/u014552756/article/details/50672638

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>

using namespace std;
struct Point
{
    int x,y;
    Point() {}
    Point(int _x,int _y)
    {
        x = _x;
        y = _y;
    }
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    int operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
    int operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
};
struct Line
{
    Point s,e;
    Line() {}
    Line(Point _s,Point _e)
    {
        s = _s;
        e = _e;
    }
};

int xmult(Point p0,Point p1,Point p2) //计算p0p1 X p0p2
{
    return (p1-p0)^(p2-p0);
}
const int MAXN = 5050;
Line line[MAXN];
int ans[MAXN];
int num[MAXN];
bool cmp(Line a,Line b)
{
    return a.s.x < b.s.x;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,m,x1,y1,x2,y2;
    while(scanf("%d",&n) == 1 && n)
    {
        scanf("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2);
        int Ui,Li;
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d",&Ui,&Li);
            line[i] = Line(Point(Ui,y1),Point(Li,y2));
        }
        line[n] = Line(Point(x2,y1),Point(x2,y2));
        sort(line,line+n+1,cmp);
        int x,y;
        Point p;
        memset(ans,0,sizeof(ans));
        while( m-- )
        {
            scanf("%d%d",&x,&y);
            p = Point(x,y);
            int l = 0,r = n;
            int tmp;
            while( l <= r)
            {
                int mid = (l + r)/2;
                if(xmult(p,line[mid].s,line[mid].e) < 0)
                {
                    tmp = mid;
                    r = mid - 1;
                }
                else l = mid + 1;
            }
            ans[tmp]++;
        }
        for(int i = 1; i <= n; i++)
            num[i] = 0;
        for(int i = 0; i <= n; i++)
            if(ans[i]>0)
                num[ans[i]]++;
        printf("Box\n");
        for(int i = 1; i <= n; i++)
            if(num[i]>0)
                printf("%d: %d\n",i,num[i]);
    }
    return 0;
}

你可能感兴趣的:(poj 2398 Toy Storage(叉积判断点是否在四边形内))