poj2318 Toys 【计算几何】

题目大意:

给出一个矩形抽屉,然后有n个板子放在其中,连接上下底。给出m个玩具的坐标(视作点),
问n+1个区间内,每个区间中有多少玩具。

解题思路:

直接二分+叉乘判断每个玩具在那个区间内即可。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<ctime>
#include<vector>
#include<queue>
#define ll long long
using namespace std;

int getint()
{
    int i=0,f=1;char c;
    for(c=getchar();(c!='-')&&(c<'0'||c>'9');c=getchar());
    if(c=='-')c=getchar(),f=-1;
    for(;c>='0'&&c<='9';c=getchar())i=(i<<3)+(i<<1)+c-'0';
    return i*f;
}

const int N=5005;
struct point
{
    int x,y;
    inline friend point operator - (const point &a,const point &b)
    {
        point res;
        res.x=a.x-b.x;
        res.y=a.y-b.y;
        return res;
    } 
    inline friend int operator * (const point &a,const point &b)
    {return a.x*b.y-a.y*b.x;}
}p;
struct node
{
    point s,t;
}board[N];
int n,m,X1,Y1,X2,Y2,cnt[N];

bool Left(point a,node b)
{
    return (b.t-a)*(b.s-a)>0;
}

int main()
{
    //freopen("lx.in","r",stdin);
    //freopen("lx.out","w",stdout);
    while(n=getint())
    {
        if(!n)break;
        memset(cnt,0,sizeof(cnt));
        m=getint(),X1=getint(),Y1=getint(),X2=getint(),Y2=getint();
        for(int i=1;i<=n;i++)
        {
            int u=getint(),l=getint();
            board[i].s.x=u,board[i].s.y=Y1;
            board[i].t.x=l,board[i].t.y=Y2;
        }
        ++n;
        board[n].s.x=X2,board[n].s.y=Y1;
        board[n].t.x=X2,board[n].t.y=Y2;
        for(int i=1;i<=m;i++)
        {
            p.x=getint(),p.y=getint();
            int l=1,r=n,id;
            while(l<=r)
            {
                int mid=l+r>>1;
                if(Left(p,board[mid]))id=mid,r=mid-1;
                else l=mid+1;
            }
            cnt[id]++;
        }
        for(int i=1;i<=n;i++)
            cout<<i-1<<": "<<cnt[i]<<'\n';
        cout<<'\n';
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值