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 } 

时间: 2024-08-28 12:25:26

TOYS的相关文章

POJ 2318 TOYS 叉积

题意: 给出一个矩形范围,给出n条线段,这n条线段一定与矩形上下边界相交且互不相交,将矩形分成n+1个划分.给出m个玩具的坐标.求每个划分放的玩具数,玩具保证不会在线段和左右边界上. 分析: 判断点是否在两条直线中间,利用叉积,如果在两条直线间,必定会有两个叉积一个小于0,一个大于0(不能把相乘小于0作为判断条件) #include <iostream> #include <cstdio> #include <cstring> using namespace std;

POJ2318 TOYS[叉积 二分]

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14433   Accepted: 6998 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 w

【POJ】2318 TOYS ——计算几何+二分

TOYS Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10281   Accepted: 4924 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 w

Codeforces-C. Tanya and Toys(水题)

In Berland recently a new collection of toys went on sale. This collection consists of 109 types of toys, numbered with integers from 1 to109. A toy from the new collection of the i-th type costs i bourles. Tania has managed to collect n different ty

【POJ】2318 TOYS(计算几何+暴力)

http://poj.org/problem?id=2318 第一次完全是$O(n^2)$的暴力为什么被卡了-QAQ(一定是常数太大了...) 后来排序了下点然后单调搞了搞..(然而还是可以随便造出让我的code变成$O(n^2)$的23333) #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <iostream> #include

[2016-03-31][codeforces][659C][Tanya and Toys]

时间:2016-03-31 23:49:13 星期四 题目编号:[2016-03-31][codeforces][659C][Tanya and Toys].md 题目大意:有$10^9$种物品,第i种物品价值i,已经用用n个物品,给m元,问最多能买多少个还没拥有的物品 分析:贪心,从最低的开始买起,假设$m = 10^9$,那么也买的物品也不超过$10^6$个,因为$\frac{(1+k)k}{k} < 10^9$ 遇到的问题:答案可能为0 #include <algorithm> #

【BZOJ1229】【USACO 2008 Nov Gold】 4.Toys sadstory 三分+贪心

sad story:我们自己oj的数据貌似有点问题.标程WA了5% 题解: 复制去Google翻译翻译结果 首先引一下VFK神犇的证明来证明一下这道题是三分.. { 我来告诉你世界的真相 = = 因为这题能最小费用最大流 每次最短路长度不降 所以是单峰的 最短路长度就是差分值.. 所以一阶导不降.. 是不是简单粗暴 你要证函数是单峰的. 当然是证斜率什么的 } 三分完初始买了多少个玩具,然后就是贪心. 首先我想说这个贪心真动规.虽然它真的是贪心. 首先先说一种错误的贪心. 就是从前往后扫,优先用

[POJ2318]TOYS

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

2016NEFU集训第n+3场 G - Tanya and Toys

Description In Berland recently a new collection of toys went on sale. This collection consists of 109 types of toys, numbered with integers from 1to 109. A toy from the new collection of the i-th type costs i bourles. Tania has managed to collect n