TOYS
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10281   Accepted: 4924

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.
 
题意:给你一个矩形,并且用n根线段将矩形格成n+1个隔间,然后再给你m个点,让你统计每个隔间内点的个数。
 
题解:这题运用的是计算几何中点与线段的位置关系,用叉积的性质来判断点在线段的左边还是右边,再用二分搜索判断点所处的位置,并统计输出。
 
AC代码:
 
  1. #include <cstdio>
  2. #include <cstring>
  3.  
  4. const int LEN = ;
  5.  
  6. struct Point //点的结构体
  7. {
  8. int x;
  9. int y;
  10. };
  11.  
  12. struct Line //线段的结构体
  13. {
  14. Point a;
  15. Point b;
  16. }line[LEN];
  17.  
  18. int ans[LEN];
  19.  
  20. int judge(Line tline, Point p3) //运用叉积的性质判断点在线段的左边还是右边
  21. {
  22. Point t1, t2;
  23. t1.x = p3.x - tline.b.x;
  24. t1.y = p3.y - tline.b.y;
  25. t2.x = tline.a.x - tline.b.x;
  26. t2.y = tline.a.y - tline.b.y;
  27. return t1.x * t2.y - t1.y * t2.x;
  28. }
  29.  
  30. int divide(int l, int r, Point toy) //二分查找
  31. {
  32. int mid = (l + r) / ;
  33. if (mid == l)
  34. return l;
  35. if (judge(line[mid], toy) < )
  36. return divide(l, mid, toy);
  37. else
  38. return divide(mid, r, toy);
  39. }
  40.  
  41. int main()
  42. {
  43. int n, m, x1, y1, x2, y2;
  44. //freopen("in.txt", "r", stdin);
  45. while(scanf("%d", &n) != EOF && n){
  46. scanf("%d %d %d %d %d", &m, &x1, &y1, &x2, &y2);
  47. memset(ans, , sizeof(ans));
  48. for(int i = ; i <= n; i++){
  49. int up, low;
  50. scanf("%d %d", &up, &low);
  51. line[i].a.x = up;
  52. line[i].a.y = y1;
  53. line[i].b.x = low;
  54. line[i].b.y = y2;
  55. }
  56. n++;
  57. line[].a.x = line[].b.x = x1;
  58. line[].b.y = line[n].b.y = y2;
  59. line[].a.y = line[n].a.y = y1;
  60. line[n].a.x = line[n].b.x = x2;
  61. for(int i = ; i < m; i++){
  62. Point toy;
  63. scanf("%d %d", &toy.x, &toy.y);
  64. ans[divide(, n, toy)]++;
  65. }
  66. for(int i = ; i < n; i++){
  67. printf("%d: %d\n", i, ans[i]);
  68. }
  69. printf("\n");
  70. }
  71. return ;
  72. }

【POJ】2318 TOYS ——计算几何+二分的更多相关文章

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

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

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

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

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

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

  4. POJ 2318 TOYS(计算几何)

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

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

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

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

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

  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 TOYS & POJ 2398 Toy Storage

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

随机推荐

  1. android 测量控件视图的方法

    在实际项目中经常要用到 测量一个控件或者视图的高,宽.然后根据这个高宽进行一些逻辑. 计算视图宽高有几种方式先简单的了解下android 视图的绘制过程会促进理解. 一.android View绘制过 ...

  2. struts2配置文件struts.xml的简介

    本文在于总结,深入研究有别人写的很好了,也没必要再去写,将在本文后面附上他们的文章地址: 一.struts2的执行过程: 二.struts2的配置文件struts.xml 下面是其三大部分includ ...

  3. delphi 修改代码补全的快捷键(由Ctrl+Space 改为 Ctrl + alt + Space)(通过修改OpenTool生效)

    delphi 的IDE快捷键与输入法切换键中突,以往的解决方法是下载一个ImeTool修改 windows 系统的快捷键 在 xp win7 都好使,但在win 10经常是修改完后,重启又失效了. 本 ...

  4. docker 镜像管理

    docker:/root# docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official bui ...

  5. KnockOutJS学习系列----(一)

    原文地址:http://www.cnblogs.com/n-pei/archive/2011/12/23/2299217.html 好几个月没去写博客了,最近也是因为项目紧张,不过这个不是借口,J. ...

  6. ID3决策树算法原理及C++实现(其中代码转自别人的博客)

    分类是数据挖掘中十分重要的组成部分.分类作为一种无监督学习方式被广泛的使用. 之前关于"数据挖掘中十大经典算法"中,基于ID3核心思想的分类算法C4.5榜上有名.所以不难看出ID3 ...

  7. 滚动视差效果——background-attachment

    滚动视差效果的实现原理是在同一个页面上将页面元素分为多层,例如可以分为背景.内容.贴图层,在滚动页面的时候让三者滚动的速度不一,从而在人的视觉上能够形成一种立体的近似效果.最近在做一个项目wiki的时 ...

  8. C#运用实例.读取csv里面的词条,对每一个词条抓取百度百科相关资料,然后存取到数据库

    第一步:首先需要将csv先装换成datatable,这样我们就容易进行对datatable进行遍历: /// 将CSV文件的数据读取到DataTable中 /// CSV文件路径 /// 返回读取了C ...

  9. 阻止JS事件冒泡传递(cancelBubble 、stopPropagation)

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx. ...

  10. IOS开发 统计XCODE 代码行数

    如果要统计ios开发代码,包括头文件的,终端命令进入项目目录下,命令如下 find . -name "*.m" -or -name "*.h" -or -nam ...