TOYS

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TOYS相关的知识,希望对你有一定的参考价值。

TOYS

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 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
 5 10
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 example illustrates, toys that fall on the boundary of the box are "in" the box.
 
计算几何的入门题。题目的意思是在一个矩形箱中放物品,其中,会用n块板隔成n+1个空间,求每个物体分别在哪个空间内。
那么,由于n比较小,直接模拟就是了,模拟的核心就是判断物品与线段(相当于直线)的位置,到底在左边还是右边.这个怎么判断?用叉积就是了。
但是尴尬的是,TLE了,没办法,多组数据。但是,我们能很快想到,二分,这是有序的呀。
技术分享
 1 #include<cmath>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #define LL long long
 6 using namespace std;
 7 int n,m,ans[10005];
 8 int uy,dy,U[10005],D[10005];
 9 int read(){
10     int x=0,f=1; char ch=getchar();
11     while (ch<0||ch>9){if (ch==-) f=-f; ch=getchar();}
12     while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar();
13     return x*f;
14 }
15 int cross(int x_0,int y_0,int x_1,int y_1){
16     return x_0*y_1-x_1*y_0;
17 }
18 int main(){
19     n=read();
20     while (n){
21         m=read(),U[0]=D[0]=read(),uy=read(),U[n+1]=D[n+1]=read(),dy=read();
22         for (int i=1; i<=n; i++) U[i]=read(),D[i]=read();
23         memset(ans,0,sizeof ans);
24         for (int i=1; i<=m; i++){
25             int x=read(),y=read();
26             int L=1,R=n+1,mid,pos;
27             while (L<=R){
28                 mid=(L+R)>>1;
29                 if (cross(U[mid]-D[mid],uy-dy,x-D[mid],y-dy)>0) R=mid-1,pos=mid; else L=mid+1;
30             }
31             ans[pos-1]++;
32         }
33         for (int i=0; i<=n; i++) printf("%d: %d\n",i,ans[i]);
34         putchar(\n);
35         n=read();
36     }
37     return 0;
38 } 
View Code

 

 





以上是关于TOYS的主要内容,如果未能解决你的问题,请参考以下文章

ruby toys.rb

POJ-2318 TOYS

[POJ2318]TOYS

TOYS

[POJ 2318]TOYS

POJ-2318 TOYS[计算几何]