poj 2318 二分+叉积计算几何基础

/*
题意:一个矩形,给出n个纸板分割成多个四边形,每个纸板只与上下边相连,再给出m个点,
这些点只会在矩形边界上或者矩形内,不会在纸板上,求n个纸板分割出的n+1个四边形分别包
含多少个点。

题解:二分+叉积;用叉积求出该点位于某个线段的左边还是右边,然后用二分求出该点在哪
两条相邻边之间。

注意:输出格式要注意一下
*/
#include <iostream>

using namespace std;

int n,x1,y1,x2,y2;

struct edge
{
    int u,l,sum;
}e[5005];

int bin(int x, int y)
{
    int top,bottom,mid;
    top = 0;
    bottom = n;
    mid = (top+bottom)/2;
    while (top <= bottom)
    {
        int cross1 = (x-e[mid].l)*(y1-y2)-(y-y2)*(e[mid].u-e[mid].l);
        int cross2 = (x-e[mid+1].l)*(y1-y2)-(y-y2)*(e[mid+1].u-e[mid+1].l);
        if (cross1 < 0)
        {
            bottom = mid - 1;
        }
        else if (cross2 > 0)
        {
            top = mid + 1;
        }
        else
        {
            return mid;
        }
        mid = (top + bottom)/2;
    }
}

int main(void)
{
    int m;
    while (cin >> n && n)
    {
        cin >> m >> x1 >> y1 >> x2 >> y2;
        for(int i=1; i<=n; i++)
        {
            cin >> e[i].u >> e[i].l;
            e[i].sum = 0;
        }
        e[0].sum = 0;
        e[0].u = e[0].l = x1;
        e[n+1].u = e[n+1].l = x2;
        int x,y;
        while (m--)
        {
            cin >> x >> y;
            e[bin(x,y)].sum++;
        }
        for(int i=0; i<=n; i++)
        {
            printf("%d: %d\n",i,e[i].sum);
        }
        cout << endl;
    }
    return 0;
}

 

posted @ 2014-03-20 23:26  辛力啤  阅读(216)  评论(0编辑  收藏  举报