POJ 2398 Toy Storage(点与直线关系 + 二分)

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

这个题目和上一篇博客题目意思几乎一样,不过这个是要求格子内装玩具个数为i的格子数

具体解法基本一致!

#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],re_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);
        }
        memset(re_ans,0,sizeof(re_ans));
        for(i=0;i<n-1;i++)
        re_ans[ans[i]]++;
        printf("Box\n");
        for(i=1;i<=n;i++){
            if(re_ans[i]!=0){
                printf("%d: %d\n",i,re_ans[i]);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值