Poj 2398 Toy Storage (叉积+二分)

一时失言乱红尘 2023-01-17 07:38 276阅读 0赞

第一道用codeblocks敲的题,在换行缩进等方面还不是很适应,居然没有变量名整体替换功能。。。。

变量名字长些可以用查找+替换,短了基本无解。。。

这一题和上一题基本一样,只不过板子没有按照顺序给出,需要排序。最后要求输出包含同样个数的玩具的格子各有多少个

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

发表评论

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

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

相关阅读

    相关 poj1064()Cable master

    //二分判断 /假定一个解并判断是否可行 题意:有n条绳子,长度分别为L[i]。如果从他们中切割出k条长度相同的绳子的话,这k条绳子每条最长能有多长

    相关 Poj 2318 TOYS (+二分)

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

    相关

    叉积   叉积的计算是线段方法的核心。考虑如图33-1(a)所示的向量p1和p2。我们可以把叉积解释为由点(0,0),p1,p2 和 p1+p2=(x1+x2,y1+y2

    相关 POJ 2318 (计算几何 )

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