Poj 2318 TOYS (叉积+二分)

青旅半醒 2023-01-17 06:21 259阅读 0赞

好久没做计算几何了,随便找道题恢复下手感,明显感觉写复杂了。。。。

应该是最后一道用vc6.0敲的题了,下一道改用codeblocks

题意:给定一个长方形箱子,中间有n条线段,将其分为n+1个区域,给定m个玩具的坐标,统计每个区域中的玩具个数。

思路:二分枚举区域,玩具与分割线的位置用叉积判断

  1. #include <cstdio>
  2. #include <cstring>
  3. const int N=5005;
  4. int n,m,x1,y11,x2,y2;
  5. int cnt[N];
  6. template<typename Type>
  7. class Point
  8. {
  9. public:
  10. Type x,y;
  11. Point(){}
  12. Point (Type _x,Type _y)
  13. {
  14. x=_x;
  15. y=_y;
  16. }
  17. Point operator-(const Point &b) const
  18. {
  19. return Point(x-b.x,y-b.y);
  20. }
  21. //调用点a的该函数
  22. //返回正值点a在向量bc的左侧
  23. //返回负值点a在向量bc的右侧
  24. //返回0点a在向量bc这条直线上
  25. Type Cross (Point b,Point c)
  26. {return (b.x-x)*(c.y-y)-(c.x-x)*(b.y-y);}
  27. };
  28. Point<int> up[N],down[N],data[N];
  29. void Init ()
  30. {
  31. int i,a,b;
  32. memset(cnt,0,sizeof(cnt));
  33. for (i=0;i<n;i++)
  34. {
  35. scanf("%d%d",&a,&b);
  36. up[i]=Point<int>(a,y11);
  37. down[i]=Point<int>(b,y2);
  38. }
  39. for (i=1;i<=m;i++)
  40. {
  41. scanf("%d%d",&a,&b);
  42. data[i]=Point<int>(a,b);
  43. }
  44. }
  45. int Solve (Point<int> cc)
  46. {
  47. int low=0,high=n,mid;
  48. while (low<=high)
  49. {
  50. mid=(low+high)>>1;
  51. if (down[mid].Cross(up[mid],cc)>0)
  52. high=mid-1;
  53. else
  54. low=mid+1;
  55. }
  56. if (low>n) low=n;
  57. return low;
  58. }
  59. int main ()
  60. {
  61. #ifdef ONLINE_JUDGE
  62. #else
  63. freopen("read.txt","r",stdin);
  64. #endif
  65. while (~scanf("%d",&n),n)
  66. {
  67. scanf("%d%d%d%d%d",&m,&x1,&y11,&x2,&y2);
  68. int i;
  69. Init ();
  70. for (i=1;i<=m;i++)
  71. cnt[Solve(data[i])]++;
  72. for (i=0;i<=n;i++)
  73. printf("%d: %d\n",i,cnt[i]);
  74. printf("\n");
  75. }
  76. return 0;
  77. }

发表评论

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

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

相关阅读

    相关 Poj 2318 TOYS (+二分)

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

    相关 POJ 2318 (计算几何 )

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