poj 2318

来源:互联网 发布:js多个请求 编辑:程序博客网 时间:2024/05/16 06:53

由于期末考试好久没写题来 ( ps:都是借口, 没写题也没干别的,天天就看捧本小破书一看一上午,看一页睡半个小时, so 还是写题吧)

  简单的计算机和入门

由于. You may assume that no toy will land exactly on a cardboard partition or outside the boundary of the box. The input is terminated by a line consisting of a single 0.

 所以我们可以确定所有的点都是在(x1,y1) ( x1,y1)(x2,y2)(x2,y1)四个点所构成的举行内部的, 而且没有点在直线上即所有的点都唯一的在一个容器内,所以这道题由点与四边形的关系变成点与直线的关系了,我们只需要判断点跟隔板的关系就好了,就是线段与点的位置关系,又因为所有的隔板都是按照顺序给的,所有可以用二分来找最合适的位置

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <cmath>


using namespace std;
#define MAXN  5555
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);
    }
    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 a, Point b)
    {
        s = a;
        e = b;
    }


} sta[MAXN];


int xmul( Point a, Point b, Point c)
{
    return (b - a) ^ ( c - a);
}


int num[MAXN];
int main()
{
    bool first = true;
    int n, m, x1, y1, x2, y2;
    while(scanf("%d",&n) != EOF && n)
    {
        scanf("%d %d %d %d %d", &m, &x1, &y1, &x2, &y2);
        memset(num, 0, sizeof(num));
        if(!first)
            printf("\n");
        int ui, li;
        for( int i = 0; i < n; i++)
        {
            scanf("%d %d",&ui, &li);
            sta[i] = Line(Point(ui,y1), Point(li, y2));
        }
        sta[n] = Line(Point(x2,y1), Point(x2, y2));


        int x, y;
        Point toy;
        for( int i = 0; i < m; i++)
        {
            scanf("%d %d",&toy.x, &toy.y);
            int l = 0;
            int r = n+1;
            int tmp;
            while( l <= r)
            {


                int mid = ( l + r) >> 1;
                if(xmul(sta[mid].s, sta[mid].e, toy) < 0)
                {
                    tmp = mid;
                    r = mid - 1;
                }
                else l = mid + 1;
            }
            num[tmp]++;
        }
        for( int i = 0; i <= n; i++)
             printf("%d: %d\n",i,num[i]);
       first = false;
    }
}

0 0