POJ 2318 TOYS(计算几何+点与直线位置关系+二分)

题目链接:http://poj.org/problem?id=2318

求出每个格子里面玩具数量
首先把起始竖线和末尾竖线加上,最后对于每个点二分判断在哪个格子里面,这个二分应该很明显

点在直线哪边可以通过X乘积判断

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
using namespace std;
#define maxn 6000
struct point{
    double x,y;
    point(double _x=0,double _y=0):x(_x),y(_y){}
}begin,end,temp;
struct line{
    point a,b;
}po[maxn];
int n,m;
double cross(point &a,point &b,point &c){
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
bool cmp(const line &a,const line &b){
    if(a.b.x != b.b.x) return a.b.x < b.b.x;
    return a.a.x < a.a.x;
}
int ans[maxn];
int find_ans(point temp){
    if(temp.x>end.x || temp.x <begin.x || temp.y>begin.y || temp.y<end.y) return 0;
    int l=0,r=n-1,mid;
    while(l<r){
        mid=(l+r)/2;
        if(l+1==r){
            ans[l]++;
            return 0;
        }
        if(cross(temp,po[mid].a,po[mid].b)>0)
        r=mid;
        else l=mid;
    }
    return 0;
}
int main(){
    int i,j,k;
    while(scanf("%d",&n),n){
        scanf("%d%lf%lf%lf%lf",&m,&begin.x,&begin.y,&end.x,&end.y);
        po[0].a=point(begin.x,end.y);
        po[0].b=begin;
        for(i=1;i<=n;i++){
            scanf("%lf%lf",&po[i].b.x,&po[i].a.x);
            po[i].a.y=end.y,po[i].b.y=begin.y;
        }
        n++;
        po[n].a=end;
        po[n].b=point(end.x,begin.y);
        n++;
        sort(po,po+n,cmp);
        memset(ans,0,sizeof(ans));
        for(i=0;i<m;i++){
            scanf("%lf%lf",&temp.x,&temp.y);
            find_ans(temp);
        }
        for(i=0;i<n-1;i++)
        printf("%d: %d\n",i,ans[i]);
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值