POJ--2318 -- TOYS [点、线基本关系] [计算几何]

来源:互联网 发布:淘宝买鞋哪家店好 编辑:程序博客网 时间:2024/06/04 00:51

 

TOYS
 
 

Time Limit: 2000MS

Memory Limit: 65536KTotal Submissions: 9023

Accepted: 4290

 

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

5 6 0 10 60 03 14 36 810 1015 301 52 12 85 540 107 94 10 0 10 100 020 2040 4060 6080 80 5 1015 1025 1035 1045 1055 1065 1075 1085 1095 100

Sample Output

0: 21: 12: 13: 14: 05: 10: 21: 22: 23: 24: 2

Hint

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

 

 

Code:

 

只把每个格格的右边一条边存在数组里,

判断的时候与这一条叉乘,如果<0说明该点应该在这条线段的顺时针方向,也就是右边;

如果>0说明该点应该在这条线段的逆时针方向,也就是左边

特别的如果=0,说明toy正好落在格格上,依提议我把它归在<0的情况里

在搜索的时候直接从第一个搜到末尾显然是非常方便的,只需要判断叉积的大小即可

为了节约时间,我用了二分搜索,当然也造成了崩溃的麻烦哭,又增加了判断条件(如下注释)

结果是我对二分搜索的性质有了更深刻的理解,记住了!

 

#include<stdio.h>#include"string.h"typedef struct par{int x1,x2;}par;par box[5005];int res[5005],n,zsx,zsy,yxx,yxy;double calc(int x1,int y1,int x2,int y2,int x3,int y3){return (x1-x2)*(y1-y3) - (y1-y2)*(x1-x3);}int land(int a,int b,int s,int e){double c1,c2,c3;int mid = (s + e)/2,temp;//printf("s:%d e:%d mid:%d\n",s,e,mid);if(s==e) return mid;c1 = calc(box[mid].x1,zsy,box[mid].x2,yxy,a,b);c2 = calc(box[mid-1].x1,zsy,box[mid-1].x2,yxy,a,b);//这两句和下面的判断条件是为了防止mid在/2的时候把最近的有可能是正确的位置给除掉了c3 = calc(box[mid+1].x1,zsy,box[mid+1].x2,yxy,a,b);//比如只剩下4和5的时候mid=4,5就不会判断了//printf("%lf   %lf   %lf\n",c1,c2,c3);if(c1<=0&&c2>=0 ) return mid;if((c1>0&&c3<=0)) return mid+1;if(c1<=0) return land(a,b,s,mid);if(c1>0) return land(a,b,mid,e);}int main(){int m,i,temp,a,b;while(scanf("%d",&n),n){memset(res,0,sizeof(res));scanf("%d%d%d%d%d",&m,&zsx,&zsy,&yxx,&yxy);box[n].x1 = box[n].x2 = yxx; for(i=0;i<n;i++){scanf("%d%d",&a,&b);//第i格存右边的边 box[i].x1 = a;box[i].x2 = b;}for(i=0;i<m;i++){scanf("%d%d",&a,&b);//取x值就好。与y无关 temp = land(a,b,0,n);//printf("temp:%d\n-----\n",temp);res[temp]++;}for(i=0;i<=n;i++)printf("%d: %d\n",i,res[i]);printf("\n");}} /*4 1 0 100 600 040 6045 6550 70100 8055 0*/
原创粉丝点击