POJ-2318-TOYS(简单计算几何,两种做法)

向右看齐 2022-03-29 17:46 370阅读 0赞

TOYS














Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19721   Accepted: 9291

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.
2318_1.jpg
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. 0: 2
  8. 1: 2
  9. 2: 2
  10. 3: 2
  11. 4: 2

Hint

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

Source

Rocky Mountain 2003

题目大意:给你一个矩形,然后再给你n条将这些矩形分为n+1个区域的线段,再给你m个点,问你这n+1个区域内的点的个数

两种做法:

第一种:暴力,直接将每个点和每条线段进行判断:只有在左边才算是落在区域内

然后要用c++交,不然会超时

代码:

  1. /*
  2. 给出矩形四个点的坐标,和m个玩具的坐标,现在从左到右给出n条两两不想交的线段
  3. 问每个区域内有多少个玩具9点)
  4. 思路1:判断点和直线的关系
  5. */
  6. #include<map>
  7. #include<stack>
  8. #include<queue>
  9. #include<string>
  10. #include<cmath>
  11. #include<cstdio>
  12. #include<cstring>
  13. #include<cstdlib>
  14. #include<iostream>
  15. #include<algorithm>
  16. using namespace std;
  17. #define ll long long
  18. #define ul unsigned long long
  19. #define bug printf("mmp\n");
  20. #define inf 0x3f3f3f3f
  21. #define esp 1e-6
  22. #define mm(a,b) memset(a,b,sizeof(a))
  23. #define T()int test,q=1,tt;scanf("%d",&test),tt=test;while(test--)
  24. const int N=1e3+10;
  25. const int maxn=1e6+100;
  26. struct point
  27. {
  28. double x,y;
  29. } num[maxn];
  30. struct line
  31. {
  32. point st,ed;///线段的端点
  33. double k,b;///直线的斜率和截距
  34. } l[maxn];
  35. int number[maxn];
  36. int main()
  37. {
  38. int m,n;
  39. point a,b,c,d;///矩形四个点
  40. while(scanf("%d",&n)&&n)
  41. {
  42. memset(number,0,sizeof(number));
  43. cin>>m>>a.x>>a.y>>c.x>>c.y;
  44. b.x=c.x,b.y=a.y;
  45. d.x=a.x,d.y=c.y;
  46. for(int i=0; i<n; i++)
  47. {
  48. l[i].st.y=a.y;
  49. l[i].ed.y=c.y;
  50. cin>>l[i].st.x>>l[i].ed.x;
  51. if(l[i].st.x==l[i].ed.x)///与矩形垂直
  52. {
  53. l[i].k=0;
  54. l[i].b=0;
  55. continue;
  56. }
  57. else///计算直线的斜率和截距
  58. {
  59. l[i].k=(l[i].ed.y-l[i].st.y)/(l[i].ed.x-l[i].st.x);
  60. l[i].b=l[i].ed.y-l[i].k*l[i].ed.x;
  61. }
  62. }
  63. l[n].k=0,l[n].b=0;///最后一条直线
  64. l[n].st=b,l[n].ed=c;
  65. double x,y;
  66. ///只能判断在左边,右边不能直接判断
  67. for(int i=0; i<m; i++)
  68. {
  69. cin>>x>>y;
  70. for(int j=0; j<=n; j++) ///逐个判断
  71. {
  72. if(l[j].k==0)///当线段垂直时比较一下该点和端点的关系
  73. {
  74. if(x<l[j].st.x)
  75. {
  76. number[j]++;
  77. break;
  78. }
  79. }
  80. else if(l[j].k>0)
  81. {
  82. if(y>l[j].k*x+l[j].b)
  83. {
  84. number[j]++;
  85. break;
  86. }
  87. }
  88. else if(l[j].k<0)
  89. {
  90. if(y<l[j].k*x+l[j].b)
  91. {
  92. number[j]++;
  93. break;
  94. }
  95. }
  96. }
  97. }
  98. for(int i=0; i<=n; i++)
  99. {
  100. printf("%d: %d\n",i,number[i]);
  101. }
  102. printf("\n");
  103. }
  104. return 0;
  105. }

第二种:利用叉积判断点与直线的关系再加上二分

代码:

  1. /*
  2. 给出矩形四个点的坐标,和m个玩具的坐标,现在从左到右给出n条两两不想交的线段
  3. 问每个区域内有多少个玩具9点)
  4. 思路2:利用向量叉积与二分
  5. */
  6. #include<map>
  7. #include<stack>
  8. #include<queue>
  9. #include<string>
  10. #include<cmath>
  11. #include<cstdio>
  12. #include<cstring>
  13. #include<cstdlib>
  14. #include<iostream>
  15. #include<algorithm>
  16. using namespace std;
  17. #define ll long long
  18. #define ul unsigned long long
  19. #define bug printf("mmp\n");
  20. #define inf 0x3f3f3f3f
  21. #define esp 1e-6
  22. #define mm(a,b) memset(a,b,sizeof(a))
  23. #define T()int test,q=1,tt;scanf("%d",&test),tt=test;while(test--)
  24. const int N=1e3+10;
  25. const int maxn=1e6+100;
  26. struct point
  27. {
  28. double x,y;
  29. } num[maxn];
  30. struct line
  31. {
  32. point st,ed;///线段的端点
  33. double k,b;///直线的斜率和截距
  34. } l[maxn];
  35. int number[maxn];
  36. double mul(point a,point b,point c)
  37. {
  38. return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
  39. }
  40. void b_search(point a,int n)
  41. {
  42. int li=0,ri=n-1;
  43. int mid;
  44. while(li<ri)
  45. {
  46. mid=(li+ri)/2;
  47. if(mul(a,l[mid].st,l[mid].ed)>0)
  48. li=mid+1;
  49. else
  50. ri=mid;
  51. }
  52. if(mul(a,l[li].st,l[li].ed)>0)
  53. number[li+1]++;
  54. else
  55. number[li]++;
  56. }
  57. int main()
  58. {
  59. int m,n;
  60. point a,b,c,d;///矩形四个点
  61. while(scanf("%d",&n)&&n)
  62. {
  63. memset(number,0,sizeof(number));
  64. cin>>m>>a.x>>a.y>>c.x>>c.y;
  65. b.x=c.x,b.y=a.y;
  66. d.x=a.x,d.y=c.y;
  67. for(int i=0; i<n; i++)
  68. {
  69. l[i].st.y=a.y;
  70. l[i].ed.y=c.y;
  71. cin>>l[i].st.x>>l[i].ed.x;
  72. }
  73. double x,y;
  74. ///只能判断在左边,右边不能直接判断
  75. for(int i=0; i<m; i++)
  76. {
  77. cin>>x>>y;
  78. point A;
  79. A.x=x,A.y=y;
  80. b_search(A,n);
  81. }
  82. for(int i=0; i<=n; i++)
  83. {
  84. printf("%d: %d\n",i,number[i]);
  85. }
  86. printf("\n");
  87. }
  88. return 0;
  89. }

发表评论

表情:
评论列表 (有 0 条评论,370人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Poj 2318 TOYS (叉积+二分)

    好久没做计算几何了,随便找道题恢复下手感,明显感觉写复杂了。。。。 应该是最后一道用vc6.0敲的题了,下一道改用codeblocks 题意:给定一个长方形箱子,中间有n条

    相关 POJ 2318 (计算几何 叉积)

    [题目链接][Link 1] 题意:给你一个被n块挡板分隔成n+1个区域的盒子,给你m个点,问你每个区域有多少个点。 分析:这道题其实就是考对叉积的应用,计算矢量叉积是与直