TOYS
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8661   Accepted: 4114

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

Hint

As the example illustrates, toys that fall on the boundary of the box are "in" the box.

Source

 
 
 
 
 
 
就是给了m个点,落在n+1个区域中,问各个区域有多少个点。
就是利用叉积去判断点在线段的哪一侧,可以二分去做,比较快。
 
  1. /************************************************************
  2. * Author : kuangbin
  3. * Email : kuangbin2009@126.com
  4. * Last modified : 2013-07-13 17:15
  5. * Filename : POJ2318TOYS.cpp
  6. * Description :
  7. * *********************************************************/
  8.  
  9. #include <iostream>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <algorithm>
  13. #include <queue>
  14. #include <map>
  15. #include <vector>
  16. #include <set>
  17. #include <string>
  18. #include <math.h>
  19.  
  20. using namespace std;
  21. struct Point
  22. {
  23. int x,y;
  24. Point(){}
  25. Point(int _x,int _y)
  26. {
  27. x = _x;y = _y;
  28. }
  29. Point operator -(const Point &b)const
  30. {
  31. return Point(x - b.x,y - b.y);
  32. }
  33. int operator *(const Point &b)const
  34. {
  35. return x*b.x + y*b.y;
  36. }
  37. int operator ^(const Point &b)const
  38. {
  39. return x*b.y - y*b.x;
  40. }
  41. };
  42. struct Line
  43. {
  44. Point s,e;
  45. Line(){}
  46. Line(Point _s,Point _e)
  47. {
  48. s = _s;e = _e;
  49. }
  50. };
  51.  
  52. int xmult(Point p0,Point p1,Point p2) //计算p0p1 X p0p2
  53. {
  54. return (p1-p0)^(p2-p0);
  55. }
  56. const int MAXN = ;
  57. Line line[MAXN];
  58. int ans[MAXN];
  59. int main()
  60. {
  61. //freopen("in.txt","r",stdin);
  62. //freopen("out.txt","w",stdout);
  63. int n,m,x1,y1,x2,y2;
  64. bool first = true;
  65. while(scanf("%d",&n) == && n)
  66. {
  67. if(first)first = false;
  68. else printf("\n");
  69. scanf("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2);
  70. int Ui,Li;
  71. for(int i = ;i < n;i++)
  72. {
  73. scanf("%d%d",&Ui,&Li);
  74. line[i] = Line(Point(Ui,y1),Point(Li,y2));
  75. }
  76. line[n] = Line(Point(x2,y1),Point(x2,y2));
  77. int x,y;
  78. Point p;
  79. memset(ans,,sizeof(ans));
  80. while( m-- )
  81. {
  82. scanf("%d%d",&x,&y);
  83. p = Point(x,y);
  84. int l = ,r = n;
  85. int tmp;
  86. while( l <= r)
  87. {
  88. int mid = (l + r)/;
  89. if(xmult(p,line[mid].s,line[mid].e) < )
  90. {
  91. tmp = mid;
  92. r = mid - ;
  93. }
  94. else l = mid + ;
  95. }
  96. ans[tmp]++;
  97. }
  98. for(int i = ; i <= n;i++)
  99. printf("%d: %d\n",i,ans[i]);
  100. }
  101. return ;
  102. }
 
 
 
 

POJ 2318 TOYS (计算几何,叉积判断)的更多相关文章

  1. POJ 2318 TOYS 利用叉积判断点在线段的那一侧

    题意:给定n(<=5000)条线段,把一个矩阵分成了n+1分了,有m个玩具,放在为位置是(x,y).现在要问第几个位置上有多少个玩具. 思路:叉积,线段p1p2,记玩具为p0,那么如果(p1p2 ...

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

    题目传送门:POJ 2318 TOYS Description Calculate the number of toys that land in each bin of a partitioned ...

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

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

  4. POJ 2318 TOYS【叉积+二分】

    今天开始学习计算几何,百度了两篇文章,与君共勉! 计算几何入门题推荐 计算几何基础知识 题意:有一个盒子,被n块木板分成n+1个区域,每个木板从左到右出现,并且不交叉. 有m个玩具(可以看成点)放在这 ...

  5. POJ 2318 TOYS (叉乘判断)

    <题目链接> 题目大意: 给出矩形4个点和n个挡板俩顶点的位置,这n个挡板将该矩形分成 n+1块区域,再给你m个点的坐标,然你输出每个区域内有几个点. 解题思路: 用叉乘即可简单判断点与直 ...

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

    TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12015   Accepted: 5792 Description ...

  7. POJ 2318 TOYS(计算几何)

    跨产品的利用率推断点线段向左或向右,然后你可以2分钟 代码: #include <cstdio> #include <cstring> #include <algorit ...

  8. TOYS - POJ 2318(计算几何,叉积判断)

    题目大意:给你一个矩形的左上角和右下角的坐标,然后这个矩形有 N 个隔板分割成 N+1 个区域,下面有 M 组坐标,求出来每个区域包含的坐标数.   分析:做的第一道计算几何题目....使用叉积判断方 ...

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

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

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

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

随机推荐

  1. 安卓WebView中接口隐患与手机挂马利用(远程命令执行)

    安卓应用存在安全漏洞,浏览网站打开链接即可中招.目前有白帽子提交漏洞表明目前安卓平台上的应用普遍存在一个安全漏洞,用户打开一个链接就可导致远程安装恶意应用甚至完全控制用户手机,目前微信,手机QQ,QV ...

  2. HDU 1372 Knight Moves【BFS】

    题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...

  3. GrepCode

    /***************************************************************************** * GrepCode * 声明: * 最近 ...

  4. linux vim 配置文件(高亮+自动缩进+行号+折叠+优化)

    点评:将一下代码copy到 用户目录下 新建文件为 .vimrc保存即可生效 如果想所有用户生效 请修改 /etc/vimrc (建议先cp一份)"===================== ...

  5. 2015-10-11 Sunday 晴 ARM学习

    基础的知识看得差不多了,linux系统相关的,最主要是c语言基础知道,还有linux系统编程,网络编程socket等相关的.这些内容最最基础的看完了,接下来我在考虑看什么呢?是看ARM以及驱动编程系列 ...

  6. Oracle 中的 TO_DATE 和 TO_CHAR 函数

    Oracle 中的 TO_DATE 和 TO_CHAR 函数oracle 中 TO_DATE 函数的时间格式,以 2008-09-10 23:45:56 为例 格式 说明 显示值 备注 Year(年) ...

  7. jQuery autoComplete 样式

    前提:使用了jQuery-ui 官网:http://jqueryui.com/autocomplete/ /*** autocomplete ***/ .ui-widget-content { bac ...

  8. NoSQL--非关系型的数据库是什么?

    NoSQL,指的是非关系型的数据库.NoSQL,意即反SQL运动,是一项全新的数据库革命性运动,早期就有人提出,发展至2009年趋势越发高涨.NoSQL的拥护者们提倡运用非关系型的数据存储,相对于目前 ...

  9. 【LeetCode】168 & 171- Excel Sheet Column Title & Excel Sheet Column Number

    168 - Excel Sheet Column Title Given a positive integer, return its corresponding column title as ap ...

  10. mybatis系列-15-查询缓存

    15.1     什么是查询缓存 mybatis提供查询缓存,用于减轻数据压力,提高数据库性能. mybaits提供一级缓存,和二级缓存. 一级缓存是SqlSession级别的缓存.在操作数据库时需要 ...