POJ2318(叉积+二分)

题意:给了m个点,落在n+1个区域中,问各个区域有多少个点。

思路:玩具的点和隔板的上下顶点连的边求叉积,如果小于0,说明点在隔板左边,用二分找每个隔板区间对应的玩具数。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=5005;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
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;
    }
};
struct Line
{
    Point s,e;
    Line() {}
    Line(Point _s,Point _e)
    {
        s = _s;
        e = _e;
    }
};
double cha(Point a,Point b,Point c) //ab^ac
{
    return (b-a)^(c-a);
}
int ans[maxn];
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    int n,m,x1,x2,y1,y2;
    while(cin>>n&&n)
    {
        cin>>m>>x1>>y1>>x2>>y2;
        Line L[maxn];
        int xs,xx;
        for(int i=0;i<n;i++)
        {
            cin>>xs>>xx;
            Point t1(xs,y1);
            Point t2(xx,y2);
            L[i]=Line(t1,t2);
        }
        L[n]=Line(Point(x2,y1),Point(x2,y2));
        int x,y,ans[maxn]={0};
        for(int i=0;i<m;i++)
        {
            cin>>x>>y;
            Point p(x,y);
            int l=0,r=n,tmp;
            while(l<=r)
            {
                int mid=(l+r)>>1;
                if(cha(p,L[mid].s,L[mid].e)<0)
                {
                    tmp=mid;
                    r=mid-1;
                }
                else
                {
                    l=mid+1;
                }
            }
            ans[tmp]++;
        }
        for(int i=0;i<=n;i++)
        {
            cout<<i<<": "<<ans[i]<<endl;
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Dilly__dally/article/details/82289348
今日推荐