题目传送门:POJ 2318 TOYS

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 when he is finished playing with them. They gave John a rectangular box to put his toys in, but John is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for John to find his favorite toys.

John's parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box. 
 
For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box.

Input

The input file contains one or more problems. The first line of a problem consists of six integers, n m x1 y1 x2 y2. The number of cardboard partitions is n (0 < n <= 5000) and the number of toys is m (0 < m <= 5000). The coordinates of the upper-left corner and the lower-right corner of the box are (x1,y1) and (x2,y2), respectively. The following n lines contain two integers per line, Ui Li, indicating that the ends of the i-th cardboard partition is at the coordinates (Ui,y1) and (Li,y2). You may assume that the cardboard partitions do not intersect each other and that they are specified in sorted order from left to right. The next m lines contain two integers per line, Xj Yj specifying where the j-th toy has landed in the box. The order of the toy locations is random. You may assume that no toy will land exactly on a cardboard partition or outside the boundary of the box. The input is terminated by a line consisting of a single 0.

Output

The output for each problem will be one line for each separate bin in the toy box. For each bin, print its bin number, followed by a colon and one space, followed by the number of toys thrown into that bin. Bins are numbered from 0 (the leftmost bin) to n (the rightmost bin). Separate the output of different problems by a single blank line.

Sample Input

  1. 5 6 0 10 60 0
  2. 3 1
  3. 4 3
  4. 6 8
  5. 10 10
  6. 15 30
  7. 1 5
  8. 2 1
  9. 2 8
  10. 5 5
  11. 40 10
  12. 7 9
  13. 4 10 0 10 100 0
  14. 20 20
  15. 40 40
  16. 60 60
  17. 80 80
  18. 5 10
  19. 15 10
  20. 25 10
  21. 35 10
  22. 45 10
  23. 55 10
  24. 65 10
  25. 75 10
  26. 85 10
  27. 95 10
  28. 0

Sample Output

  1. 0: 2
  2. 1: 1
  3. 2: 1
  4. 3: 1
  5. 4: 0
  6. 5: 1
  7.  
  8. 0: 2
  9. 1: 2
  10. 2: 2
  11. 3: 2
  12. 4: 2
  13.  
  14. 题目大意:
  15.  
  16.   给你一个盒子,里面有n个隔板,n+1个区间:0~n 里面放m个物品,问每个区间有多少个物品。
  17.  
  18. 解题思路:
      
  1.   对于每个物品都进行二分来查找区间,通过叉积来判断点与直线的位置关系,进而确定这个物品在哪个区间。
    (我之前是想分别将线段和点进行排序然后依次比较就行了 太天真了...
  1.  
  1. #include<cmath>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. const int N = +;
  7. int num[N];
  8. struct point
  9. {
  10. double x,y;
  11. point(double a = , double b = ) { x = a, y = b; }
  12. double operator ^(const point& b) const { return x * b.y - y * b.x; }/// 叉积
  13. point operator - (const point& b) const { return point(x - b.x, y - b.y); }
  14. }p;
  15. struct line
  16. {
  17. int id;
  18. point s,e;
  19. }l[N];
  20.  
  21. bool _is(line c,point p)///判断点是否在直线右边
  22. {
  23. point p1=c.s-p,p2=c.e-p;
  24. double ans=p1^p2;
  25. if (ans>) return true;
  26. return false;
  27. }
  28. int main()
  29. {
  30. int n,m;
  31. point b,d;
  32. double u,v;
  33. while(~scanf("%d",&n)&&n)
  34. {
  35. memset(num,,sizeof(num));
  36. scanf("%d%lf%lf%lf%lf",&m,&b.x,&b.y,&d.x,&d.y);
  37. l[].s=b;l[].e.x=b.x;l[].e.y=d.y;
  38. l[n+].s.x=d.x;l[n+].s.y=b.y;l[n+].e=d;
  39. l[n+].id=n+;
  40. for (int i=;i<=n;i++)
  41. {
  42. scanf("%lf%lf",&u,&v);
  43. l[i].s.x=u,l[i].s.y=b.y;
  44. l[i].e.x=v,l[i].e.y=d.y;
  45. l[i].id=i;
  46. }
  47. for (int i=;i<m;i++)
  48. {
  49. scanf("%lf%lf",&p.x,&p.y);
  50. int L=,R=n+;
  51. while(L<R)
  52. {
  53. int mid=(L+R)/;
  54. if (_is(l[mid],p)) L=mid+;
  55. else R=mid-;
  56. }
  57. while (!_is(l[L],p)) --L;
  58. num[L]++;
  59. }
  60. for (int i=;i<=n;i++) printf("%d: %d\n",i,num[i]);
  61. printf("\n");
  62. }
  63. return ;
  64. }
  1. 同类型题:Toy Storage POJ - 2398
  1. #include<cmath>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. const int N = +;
  7. int num1[N],num2[N];
  8. struct point
  9. {
  10. double x,y;
  11. point(double a = , double b = ) { x = a, y = b; }
  12. double operator ^(const point& b) const { return x * b.y - y * b.x; }/// 叉积
  13. point operator - (const point& b) const { return point(x - b.x, y - b.y); }
  14. }p;
  15. struct line
  16. {
  17. int id;
  18. point s,e;
  19. }l[N];
  20. bool cmp(line a,line b)
  21. {
  22. return a.s.x<b.s.x;
  23. }
  24. bool _is(line c,point p)///判断点是否在直线右边
  25. {
  26. point p1=c.s-p,p2=c.e-p;
  27. double ans=p1^p2;
  28. if (ans>) return true;
  29. return false;
  30. }
  31. int main()
  32. {
  33. int n,m;
  34. point b,d;
  35. double u,v;
  36. while(~scanf("%d",&n)&&n)
  37. {
  38. memset(num1,,sizeof(num1));
  39. memset(num2,,sizeof(num2));
  40. scanf("%d%lf%lf%lf%lf",&m,&b.x,&b.y,&d.x,&d.y);
  41. l[].s=b;l[].e.x=b.x;l[].e.y=d.y;
  42. l[n+].s.x=d.x;l[n+].s.y=b.y;l[n+].e=d;
  43. l[n+].id=n+;
  44. for (int i=;i<=n;i++)
  45. {
  46. scanf("%lf%lf",&u,&v);
  47. l[i].s.x=u,l[i].s.y=b.y;
  48. l[i].e.x=v,l[i].e.y=d.y;
  49. l[i].id=i;
  50. }
  51. sort(l+,l+n+,cmp);//与上题比多了个排序,因为它的输入不是按顺序的
  52. for (int i=;i<m;i++)
  53. {
  54. scanf("%lf%lf",&p.x,&p.y);
  55. int L=,R=n+;
  56. while(L<R)
  57. {
  58. int mid=(L+R)/;
  59. if (_is(l[mid],p)) L=mid+;
  60. else R=mid-;
  61. }
  62. while (!_is(l[L],p)) --L;
  63. num1[L]++;
  64. }
  65. printf("Box\n");
  66. for (int i=;i<=n;i++) num2[num1[i]]++;
  67. //这题所求有点不同
  68. for (int i=;i<=n;i++)
  69. if (num2[i]) printf("%d: %d\n",i,num2[i]);
  70. }
  71. return ;
  72. }

POJ 2318 TOYS(叉积+二分)的更多相关文章

  1. POJ 2318 TOYS (叉积+二分)

    题目: Description Calculate the number of toys that land in each bin of a partitioned toy box. Mom and ...

  2. 2018.07.03 POJ 2318 TOYS(二分+简单计算几何)

    TOYS Time Limit: 2000MS Memory Limit: 65536K Description Calculate the number of toys that land in e ...

  3. POJ 2318 TOYS 叉积

    题目大意:给出一个长方形盒子的左上点,右下点坐标.给出n个隔板的坐标,和m个玩具的坐标,求每个区间内有多少个玩具. 题目思路:利用叉积判断玩具在隔板的左方或右方,并用二分优化查找过程. #includ ...

  4. poj 2318 TOYS (二分+叉积)

    http://poj.org/problem?id=2318 TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 101 ...

  5. 向量的叉积 POJ 2318 TOYS & POJ 2398 Toy Storage

    POJ 2318: 题目大意:给定一个盒子的左上角和右下角坐标,然后给n条线,可以将盒子分成n+1个部分,再给m个点,问每个区域内有多少各点 这个题用到关键的一步就是向量的叉积,假设一个点m在 由ab ...

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

    链接:poj 2318 题意:有一个矩形盒子,盒子里有一些木块线段.而且这些线段坐标是依照顺序给出的. 有n条线段,把盒子分层了n+1个区域,然后有m个玩具.这m个玩具的坐标是已知的,问最后每一个区域 ...

  7. 简单几何(点与线段的位置) POJ 2318 TOYS && POJ 2398 Toy Storage

    题目传送门 题意:POJ 2318 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况 分析:点和线段的位置判断可以用叉积判断.给的线段是排好序的,但是点是无序的,所以可以用二分优化 ...

  8. POJ 2318 TOYS && POJ 2398 Toy Storage(几何)

    2318 TOYS 2398 Toy Storage 题意 : 给你n块板的坐标,m个玩具的具体坐标,2318中板是有序的,而2398无序需要自己排序,2318要求输出的是每个区间内的玩具数,而231 ...

  9. POJ 2318/2398 叉积性质

    2318 2398 题意:给出n条线将一块区域分成n+1块空间,再给出m个点,询问这些点在哪个空间里. 思路:由于只要求相对位置关系,而对具体位置不关心,那么易使用叉积性质得到相对位置关系(左侧/右侧 ...

随机推荐

  1. Laravel根据Ip获取国家,城市信息

    https://blog.csdn.net/zhezhebie/article/details/79097133 1.安装: composer require geoip2/geoip2:~2.0 2 ...

  2. [转][ASP.NET Core 3框架揭秘] 跨平台开发体验: Windows [中篇]

    我们在<上篇>利用dotnet new命令创建了一个简单的控制台程序,接下来我们将它改造成一个ASP.NET Core应用.一个ASP.NET Core应用构建在ASP.NET Core框 ...

  3. 【DCN】端口与地址绑定技术

    端口与地址绑定技术   与AM技术不同之处在于,AM端口下绑定的MAC或IP能够通信,不限制绑定的MAC在其它接口下通信.   开启MAC-CPU学习模式 mac-address-learning c ...

  4. Python--day70--csrf简单用法、 跨站请求伪造和csrf_token使用

    1,csrf简单用法 2,Django里面的setting加入了防跨站伪造:这段代码帮你生成特殊字符串,帮你塞到html页面中来 3,csrf_token使用:

  5. H3C 配置高级ACL

  6. 2018-8-10-win10-uwp-win2d-使用-Path-绘制界面

    title author date CreateTime categories win10 uwp win2d 使用 Path 绘制界面 lindexi 2018-08-10 19:17:19 +08 ...

  7. 使用vuex来管理数据

    最近一直工作比较忙,博客已经鸽了好久了,趁着今天是周末,写点东西吧 使用vuex来管理数据 最近一直在用vue做项目,但是却从来没真正去用过vuex,因为一直感觉很复杂,其实真正去研究一下啊,就会发现 ...

  8. Hamcrest匹配器框架

    其实在之前的文章中已经使用过 Hamcrest 匹配器框架,本篇文章将系统的介绍它的使用. 为什么要用Hamcrest匹配器框架 Hamcrest是一款软件测试框架, 可以通过现有的匹配器类检查代码中 ...

  9. Linux 内核 usb_control_msg 接口

    usb_control_msg 函数就像 usb_bulk_msg 函数, 除了它允许一个驱动发送和结束 USB 控制信息: int usb_control_msg(struct usb_device ...

  10. 11大Java开源中文分词器的使用方法和分词效果对比,当前几个主要的Lucene中文分词器的比较

    本文的目标有两个: 1.学会使用11大Java开源中文分词器 2.对比分析11大Java开源中文分词器的分词效果 本文给出了11大Java开源中文分词的使用方法以及分词结果对比代码,至于效果哪个好,那 ...