POJ 2398- Toy Storage
还是个比较适合入门的基础题,和POJ 2318相似—>POJ 2318 题解
题目:
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 consists of a number of cases. The first line consists of six integers n, m, x1, y1, x2, y2. The number of cardboards to form the partitions is n (0 < n <= 1000) and the number of toys is given in m (0 < m <= 1000). 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 each consists of two integers Ui Li, indicating that the ends of the ith cardboard is at the coordinates (Ui, y1) and (Li, y2). You may assume that the cardboards do not intersect with each other. The next m lines each consists of two integers Xi Yi specifying where the ith toy has landed in the box. You may assume that no toy will land on a cardboard.
A line consisting of a single 0 terminates the input.
output
For each box, first provide a header stating “Box” on a line of its own. After that, there will be one line of output per count (t > 0) of toys in a partition. The value t will be followed by a colon and a space, followed the number of partitions containing t toys. Output will be sorted in ascending order of t for each box.
Sample Input
4 10 0 10 100 0
20 20
80 80
60 60
40 40
5 10
15 10
95 10
25 10
65 10
75 10
35 10
45 10
55 10
85 10
5 6 0 10 60 0
4 3
15 30
3 1
6 8
10 10
2 1
2 8
1 5
5 5
40 10
7 9
0
Sample Output
Box
2: 5
Box
1: 4
2: 1
题目大意:
n条线段将一个矩形分成n+1块,计算有t 个点的盒子有几个,按t从小到大输出。
题目分析:
这个就是一计算几何的模板题,我们首先要明白,向量的叉乘可以用来判断点在这个线的左边还是右边,这样我们就可以利用点,和这个线的单位向量来判断这个玩具在哪个盒子里了,
但是如果先做过POJ2318的朋友可能没有注意到,这个和 POJ2318 略有不同,这个分割箱子的线不是按从左到右的顺序输入的,所以我们还需要给线排序,(我就是没有注意的到这点,得到了一个 WA,注意到这个以后,我们就可以开心的输出了,但是还有一点不同的是 输出完一个样例后,没有空一行。
AC代码:
#include<iostream>
#include<cmath>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,m,x1,y3,x2,y2,u,v;
struct point{
double x,y;
point(){}
point(double a,double b){x=a,y=b;}
};
struct Line{
point star,ve;
}L[5005];
int cmp(struct Line a,struct Line b){
return (a.star.x<b.star.x);
}
int ans[5005],box[5005];
int cj(point a,point b) { return a.x*b.y-a.y*b.x; }
int main(){
while(scanf("%d%d%d%d%d%d",&n,&m,&x1,&y3,&x2,&y2)!=EOF){
if (n == 0) break;
memset(ans,0,sizeof(ans));
memset(box,0,sizeof(box));
for(int i=1;i<=n;i++){
scanf("%d%d",&u,&v);
L[i].star=point(v,y2);
L[i].ve=point(u-v,y3-y2);
}//保存直线的起始位置和单位向量。
sort(L+1,L+n+1,cmp);//对线段进行一下排序
for(int i=1;i<=m;i++){
scanf("%d%d",&u,&v);
int l=1,r=n+1;
while(l<r){
int mid = (l+r)/2;
point p(u-L[mid].star.x,v-L[mid].star.y);
if(cj(p,L[mid].ve)>0)
l = mid+1;
else
r=mid;
}
ans[l-1]++;
}//这之前求每个小格子有几个玩具的思路于POJ2318完全一样
printf("Box\n");
for(int i=0;i<=n;i++){
box[ans[i]]++;
}按每个小盒子里玩具的数量存储道box数组。
for (int i=1; i<=n; i++){
if(box[i]>0)
printf("%d: %d\n",i,box[i]);
}
}
return 0;
}
我来要赞了,如果觉得解释还算详细,可以学到点什么的话,点个赞再走吧
欢迎各位路过的dalao 指点,提出问题。