TOYS
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17182 | Accepted: 8195 |
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.
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.
题意:给出一个矩形,和n条线的两个端点的位置,输入m个点的坐标,问最后每个区域分别落有多少个点.
思路:只要可以判断点与直线的位置关系就好,这里有两个方法.
①求出直线的方程,然后分别判断每个点落在那个区域内
②用向量叉乘的方法直接判断点在直线的左右边
法①步骤,先求出每条线的k与b,然后带入每个点的x值,与y相比较,看看此点在直线上边还是下边.
法②代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
const double esp = 1e-7;
const int ff = 0x3f3f3f3f;
struct node
{
int x,y;
};
struct line
{
node u,d;
} l[5200];
int n,m,x1,yy1,x2,y2;
int ans[5200];
int fork(node p0,node p1,node p2)
{
return (p0.x-p1.x)*(p2.y-p1.y)-(p2.x-p1.x)*(p0.y-p1.y);//计算向量叉乘,小于0点在直线右边,大于0左边
}
void find(node p)
{
int i;
for(i = 1;i<= n;i++)
if(fork(p,l[i].u,l[i].d)> 0)//寻找第一条点在线左边的线
break;
ans[i-1]++;
}
void init()
{
mem(ans,0);
mem(l,0);
}
int main()
{
while(~scanf("%d",&n)&&n)
{
init();
scanf("%d %d %d %d %d",&m,&x1,&yy1,&x2,&y2);
for(int i = 1;i<= n;i++)
{
int a,b;
cin>>a>>b;
l[i].u.x = a;//存入线的两个端点
l[i].u.y = yy1;
l[i].d.x = b;
l[i].d.y = y2;
}
for(int i = 1;i<= m;i++)
{
int x,y;
scanf("%d %d",&x,&y);
node tmp;
tmp.x =x;
tmp.y = y;
find(tmp);
}
for(int i = 0;i<= n;i++)
printf("%d: %d\n",i,ans[i]);
printf("\n");
}
return 0;
}