POJ 2318 TOYS

这题计算几何的部分还是比较简单的,重点是那个二分有点麻烦(大牛忽略),每次写二分自己都得用笔模拟一番,然后才能确定。

因为y1,y2是公共的,所以存储的时候线段的时候只要存储x的坐标就可以了。然后就是判断是在右边还是在左边。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=5005;
struct Line
{
    int x1,x2;
}line[N];
int x1,x2,y1,y2,n,m,num[N];
int judge(int x,int y,int id)
{
   int a1=line[id].x1-x,a2=line[id].x2-x;
   int b1=y1-y,b2=y2-y;
   return a1*b2-a2*b1;
}
int main()
{
    int x,y,k=0;
    while(scanf("%d",&n),n!=0)
    {
        scanf("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2);
        for(int i=1;i<=n;i++)
            scanf("%d%d",&line[i].x1,&line[i].x2);
        memset(num,0,sizeof(num));
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&x,&y);
            if(judge(x,y,1)<0)//这里要注意一下
            {
                num[0]++;
                continue;
            }
            if(judge(x,y,n)>0)//这里也要注意,通过画图可以发现,这里需要单独讨论一下。
            {
                num[n]++;
                continue;
            }
            int l=1,r=n;//这里是二分
            while(r-l>1)
            {
                int mid=(l+r)/2;
                if(judge(x,y,mid)<0)
                    r=mid;
                else
                    l=mid;
            }
            num[l]++;
        }
        if(k>0)
            printf("\n");
        k++;
        for(int i=0;i<=n;i++)
            printf("%d: %d\n",i,num[i]);
    }
    return 0;
}
时间: 2024-10-09 20:28:15

POJ 2318 TOYS的相关文章

POJ 2318 TOYS(叉积+二分or暴力)

题目链接:POJ 2318 TOYS [写在前面]前几天跟队友分了方向,学渣开始进行计算几何的专题了,真是脑壳有点痛啊.但是我想做多了就没这么坑爹了 [题意]大体意思就是给你一个矩形,有被若干直线分成N个格子,给出M个点的坐标,问你每个点位于哪个格子中. [思路]其实就是点在凸四边形内的判断,然后就可以利用叉积的性质,当然可以用暴力枚举也可以过,但是时间复杂度有点高,最好是用二分求解.(一直觉得二分真是牛逼啊) 下面贴AC代码,用二分219MS就过了: 1 /* 2 ** POJ 2318 TO

poj 2318 TOYS &amp; poj 2398 Toy Storage (叉积)

链接:poj 2318 题意:有一个矩形盒子,盒子里有一些木块线段,并且这些线段坐标是按照顺序给出的, 有n条线段,把盒子分层了n+1个区域,然后有m个玩具,这m个玩具的坐标是已知的,问最后每个区域有多少个玩具 分析:从左往右,直到判断玩具是否在线段的逆时针方向为止,这个就需要用到叉积,当然可以用二分查找优化. 叉积:已知向量a(x1,y1),向量b(x2,y2),axb=x1*y2-x2*y1, 若axb>0,a在b的逆时针方向,若axb<0,则a在b的顺时针方向 注:每组数据后要多空一行

POJ 2318 TOYS 叉积的应用

A - TOYS Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2318 Appoint description:  lijunle  (2011-07-18)System Crawler  (2016-05-08) Description Calculate the number of toys that land in each b

poj 2318 TOYS 2012-01-11

http://poj.org/problem?id=2318 ___________________________________________________ 题目大意:一个箱子用木板分成几个区间,给一些玩具的坐标,求各个区间有几个玩具. 对于每一个玩具,因为输入的区间是有序的,所以采用二分木板,判断一个玩具在哪个区间. ___________________________________________________ 1 Program Stone; 2 var i,n,m,x1,x

POJ 2318 TOYS 叉积

题意: 给出一个矩形范围,给出n条线段,这n条线段一定与矩形上下边界相交且互不相交,将矩形分成n+1个划分.给出m个玩具的坐标.求每个划分放的玩具数,玩具保证不会在线段和左右边界上. 分析: 判断点是否在两条直线中间,利用叉积,如果在两条直线间,必定会有两个叉积一个小于0,一个大于0(不能把相乘小于0作为判断条件) #include <iostream> #include <cstdio> #include <cstring> using namespace std;

POJ 2318 TOYS 叉积应用

点击打开链接 TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11078   Accepted: 5312 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys

poj 2318 TOYS(计算几何 点与线段的关系)

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12015   Accepted: 5792 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w

poj 2318 TOYS (点与线段位置关系判断)

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10848   Accepted: 5206 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w

poj 2318 TOYS(判点与线段的关系)

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11643   Accepted: 5616 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w

poj 2318 TOYS(计算几何)

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13159   Accepted: 6357 Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and dad have a problem - their child John never puts his toys away w