题目连接: http://poj.org/problem?id=2318     http://poj.org/problem?id=2398

两题类似的题目,2398是2318的升级版。

题目大概是说,有一个矩形的柜子,中间有一些隔板。告诉你每个隔板的坐标,还有一些玩具的坐标,统计玩具在哪个格子里。

这题的思路很简单,如果玩具在某个隔板的左边和右边叉乘的正负是不同的。如图:

图中点P在线段CD的左边,则向量PC和向量PD叉乘结果小于0。反之P在AB的左边,向量PA叉乘PB大于0。

因此利用这个性质以及排序好的线段列表,通过二分思想,可以快速知道点在哪个格子内。

代码:

  1. #include<stdio.h>
  2. #include<math.h>
  3. #define PI 3.14159265358979323846
  4. #define MAX(x,y) ((x)>(y)?(x):(y))
  5. #define MIN(x,y) ((x)<(y)?(x):(y))
  6. #define ABS(x) (((x)>0)?(x):(-(x)))
  7. #define SIGN(x) (((x)<0)?-1:1)
  8. #define EPS 0.000001/*精度控制*/
  9. #define N 5005
  10. /*坐标的定义*/
  11. typedef double coo;/*int*/
  12. /*点、向量*/
  13. typedef struct POINT
  14. {
  15. coo x,y;
  16. }point,vector;
  17. /*线段*/
  18. typedef struct SEGMENT
  19. {
  20. point p1,p2;/*p[2];*/
  21. }segment;
  22. /*判相等*/
  23. int is_equel(double a,double b)
  24. {
  25. double c=ABS(a-b);
  26. if(c<=EPS) return ;/*相等*/
  27. else return ;/*不相等*/
  28. }
  29. /*向量的减法p1-p2*/
  30. vector vector_minus(vector p1,vector p2)
  31. {
  32. vector p;
  33. p.x=p1.x-p2.x;
  34. p.y=p1.y-p2.y;
  35. return p;
  36. }
  37. /*向量叉乘*/
  38. double cross_product(vector p1,vector p2)
  39. {/*x1y2-x2y1*/
  40. return p1.x*p2.y-p1.y*p2.x;
  41. }
  42. int bijiao(segment s1,segment s2)/*比较两个线段的位置< */
  43. {
  44. if(s1.p1.x<s2.p1.x) return ;
  45. if(s1.p1.x==s2.p1.x&&s1.p2.x<s2.p2.x) return ;
  46. return ;
  47. }
  48. int Partition(segment r[],int low,int high)/*升序*/
  49. /*返回支点最终位置*/
  50. {
  51. segment x;/*类型具体*/
  52. if(low>high) return ;
  53. if(low==high) return low;
  54. x=r[low];
  55. while(low<high)
  56. {
  57. while(low<high&&bijiao(x,r[high])) high--; /*<*/
  58. if(low<high){r[low]=r[high];low++;}
  59. while(low<high&&bijiao(r[low],x)) low++; /*>*/
  60. if(low<high){r[high]=r[low];high--;}
  61. }
  62. r[low]=x;
  63. return low;
  64. }
  65. void Quick_sort(segment r[],int m,int n) /*排序从r[m]到r[n]*/
  66. {
  67. int i;
  68. if(m>=n) return;
  69. i=Partition(r,m,n);
  70. Quick_sort(r,m,i-);
  71. Quick_sort(r,i+,n);
  72. }
  73.  
  74. int BinSearch(segment a[],int n,point k)/*在有序的数组a[0]~a[n-1]中查找k元素*/
  75. {
  76. int low=,high=n-,mid;
  77. while(low<=high)
  78. {
  79. mid=low+((high-low)/);
  80. if(cross_product(vector_minus(a[mid].p1,k),vector_minus(a[mid].p2,k))<) high=mid-;/*线段在右边*/
  81. else low=mid+;
  82. }
  83. if(low>high) return high;
  84. }
  85. int main()
  86. {
  87. int n,m,i,a[N],b[N];
  88. double x1,x2,y1,y2;
  89. point p;
  90. segment s[N];
  91. while()
  92. {
  93. scanf("%d",&n);
  94. if(n==) break;
  95. scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);
  96. s[].p1.x=x1;
  97. s[].p1.y=y1;
  98. s[].p2.x=x1;
  99. s[].p2.y=y2;
  100. for(i=;i<=n;i++)
  101. {
  102. scanf("%lf%lf",&s[i].p1.x,&s[i].p2.x);
  103. s[i].p1.y=y1;
  104. s[i].p2.y=y2;
  105. }
  106. s[i].p1.x=x2;
  107. s[i].p1.y=y1;
  108. s[i].p2.x=x2;
  109. s[i].p2.y=y2;
  110. Quick_sort(s,,n+);
  111. for(i=;i<N;i++) a[i]=;
  112. for(i=;i<m;i++)
  113. {
  114. scanf("%lf%lf",&p.x,&p.y);
  115. a[BinSearch(s,n+,p)]++;
  116. }
  117. for(i=;i<N;i++) b[i]=;
  118. for(i=;i<N;i++) b[a[i]]++;
  119. printf("Box\n");
  120. for(i=;i<N;i++) if(b[i]) printf("%d: %d\n",i,b[i]);
  121. }
  122. return ;
  123. }

PKU 2398

【解题报告】PKU 2318 TOYS AND PKU 2398 Toy Storage的更多相关文章

  1. poj 2318 TOYS &amp; poj 2398 Toy Storage (叉积)

    链接:poj 2318 题意:有一个矩形盒子,盒子里有一些木块线段.而且这些线段坐标是依照顺序给出的. 有n条线段,把盒子分层了n+1个区域,然后有m个玩具.这m个玩具的坐标是已知的,问最后每一个区域 ...

  2. POJ 2318 TOYS && POJ 2398 Toy Storage(几何)

    2318 TOYS 2398 Toy Storage 题意 : 给你n块板的坐标,m个玩具的具体坐标,2318中板是有序的,而2398无序需要自己排序,2318要求输出的是每个区间内的玩具数,而231 ...

  3. poj 2398 Toy Storage(计算几何)

    题目传送门:poj 2398 Toy Storage 题目大意:一个长方形的箱子,里面有一些隔板,每一个隔板都可以纵切这个箱子.隔板将这个箱子分成了一些隔间.向其中扔一些玩具,每个玩具有一个坐标,求有 ...

  4. POJ 2398 - Toy Storage 点与直线位置关系

    Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5439   Accepted: 3234 Descr ...

  5. POJ 2398 Toy Storage(计算几何,叉积判断点和线段的关系)

    Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3146   Accepted: 1798 Descr ...

  6. poj 2398 Toy Storage(计算几何 点线关系)

    Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4588   Accepted: 2718 Descr ...

  7. 2018.07.04 POJ 2398 Toy Storage(二分+简单计算几何)

    Toy Storage Time Limit: 1000MS Memory Limit: 65536K Description Mom and dad have a problem: their ch ...

  8. POJ 2398 Toy Storage (叉积判断点和线段的关系)

    题目链接 Toy Storage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4104   Accepted: 2433 ...

  9. 简单几何(点与线段的位置) POJ 2318 TOYS && POJ 2398 Toy Storage

    题目传送门 题意:POJ 2318 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况 分析:点和线段的位置判断可以用叉积判断.给的线段是排好序的,但是点是无序的,所以可以用二分优化 ...

随机推荐

  1. ExtJs之VTYPE验证

    <!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...

  2. Linux操作系统下的Sudo命令

    查看.修改或者执行某些命令需要root用户的权限,如果不想直接切换到root用户,就可以使用sudo命令.sudo命令用于针对单个命令授予临时权限.sudo仅在需要时授予用户权限,减少了用户因为错误执 ...

  3. 【hdu2815-Mod Tree】高次同余方程-拓展BadyStepGaintStep

    http://acm.hdu.edu.cn/showproblem.php?pid=2815 题意:裸题... 关于拓展BSGS的详细解释我写了一篇博文:http://www.cnblogs.com/ ...

  4. Project Euler 109 :Darts 飞镖

    Darts In the game of darts a player throws three darts at a target board which is split into twenty ...

  5. Spring笔记——使用Spring进行面向切面(AOP)编程

    要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间: =================== Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种: 1. 基于XM ...

  6. http://blog.csdn.net/hguisu/article/details/7533759

    http://blog.csdn.net/hguisu/article/details/7533759

  7. Android 使用全局变量传递数据

    使用全局变量传递数据,所谓的全局变量类似于jee开发中的application变量.申明后,全局调用.只有当内存被清理后,才被销毁.否则一直可以调用. 还是使用点击一个button,传递一个数据到另一 ...

  8. SQL SERVER ->> Data Compression

    最近做了一个关于数据压缩的项目,要把整个SQL SERVER服务器下所有的表对象要改成页压缩.于是趁此机会了解了一下SQL SERVER下压缩技术. 这篇文章几乎就是完全指导手册了 https://t ...

  9. json 得到时分秒为00:00:00,拿不到时分秒 解决办法

    数据库查询时间没有了时分秒的解决办法        问题出处,公司一个项目中使用动态sql方式查询Oracle数据库,在展示时Date类型字段只展示日期,无时分秒.        分析:        ...

  10. Win API 内存整理

    记得我的笔记本上曾经安装了一款名为内存整理大师的软件,当时觉得挺好用而且挺NB的,就是导致开机启动有点慢. 当时我就在想,内存整理是怎么实现的?不过那是水平实在是不怎么样,估计连windows程序的消 ...