Description
Calculate thenumber 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 heis finished playing with them. They gave John a rectangular box to put his toysin, but John is rebellious and obeys his parents by simply throwing his toysinto the box. All the toys
get mixed up, and it is impossible for John to findhis favorite toys.
John's parents came up with the following idea. They put cardboard partitionsinto the box. Even if John keeps throwing his toys into the box, at least toysthat get thrown into different bins stay separated. The following diagram showsa top view of an example
toy box.
For this problem, you are asked to determine how many toys fall into eachpartition as John throws them into the toy box.
Input
The input filecontains one or more problems. The first line of a problem consists of sixintegers, 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 coordinatesof 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, UiLi, indicating that the ends of the i-th cardboard partition is at thecoordinates (Ui,y1) and (Li,y2). You may assume that the cardboard partitionsdo not intersect each other and that they are specified in sorted order fromleft to right. The next m lines contain two integers per line, Xj Yj specifyingwhere the j-th toy has landed in the box. The order of the toy locations israndom. You may assume that no toy will land exactly on a cardboard partitionor outside the boundary of the box. The input is terminated by a lineconsisting of a single 0.
Output
The output foreach problem will be one line for each separate bin in the toy box. For eachbin, print its bin number, followed by a colon and one space, followed by thenumber of toys thrown into that bin. Bins are numbered from 0 (the leftmostbin) to n (the rightmost bin). Separate the output of different problems by a singleblank line.
Sample Input
5 6 0 10 60 0
3 1
4 3
6 8
10 10
15 30
1 5
2 1
2 8
5 5
40 10
7 9
4 10 0 10 100 0
20 20
40 40
60 60
80 80
510
15 10
25 10
35 10
45 10
55 10
65 10
75 10
85 10
95 10
0
Sample Output
0: 2
1: 1
2: 1
3: 1
4: 0
5: 1
0: 2
1: 2
2: 2
3: 2
4: 2
Hint
As the exampleillustrates, toys that fall on the boundary of the box are "in" thebox.
Source
題目大意:給你一個紙箱 ,用n條線把紙箱分成n+1個區域,然後給你玩具的點的座標,讓你計算每個區域有多少玩具,要注意,每條直線只給你兩個橫座標,對於每一條直線,第一個橫座標的縱座標是第一行輸入y1,第二個橫座標的縱座標是第一行輸入的y2。
計算幾何簡單應用,用叉乘判斷點(玩具的座標)在直線(分割線)的哪一側,如果在左側,就++
#include<stdio.h>
#include<string.h>
int ans[5100];
int n,m,x1,y1,x2,y2;
struct point{
int x1,x2;
}p[5005];
void zuoce(int x,int y) //判斷是否在直線左側
{
int i;
for(i=0;i<=n;i++)
{
if((x-p[i].x2)*(y1-y2)-(p[i].x1-p[i].x2)*(y-y2)<0) //叉乘判斷
break; //第一次滿足就退出循環
}
ans[i]++; //區域內的點數++
}
int main()
{
int i;
while(scanf("%d",&n)&&n)
{
memset(ans,0,sizeof(ans));
scanf("%d %d %d %d %d",&m,&x1,&y1,&x2,&y2);
for(i=0;i<n;i++)
{
scanf("%d %d",&p[i].x1,&p[i].x2);
}
p[n].x1=p[n].x2=x2; //把箱子最右邊的邊界線加進去
int x,y;
for(i=0;i<m;i++)
{
scanf("%d %d",&x,&y);
zuoce(x,y);
}
for(i=0;i<=n;i++)
{
printf("%d: %d\n",i,ans[i]);
}
printf("\n");
}
}