题目链接:
题目描述:
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, 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 03 14 36 810 1015 301 52 12 85 540 107 94 10 0 10 100 020 2040 4060 6080 80 5 1015 1025 1035 1045 1055 1065 1075 1085 1095 100
Sample Output
0: 21: 12: 13: 14: 05: 10: 21: 22: 23: 24: 2
Hint
As the example illustrates, toys that fall on the boundary of the box are "in" the box.
题目大意:
一个矩形内被隔板分成若干区域,给出点坐标,判断在哪个区域内
思路:
把隔板化作向量,由底边指向上边,第i条边对应的向量记为 ve[i]
第i条边的底边上的点指向p的向量记为 tmp
判断点p与第i条边的位置关系,只需要看 tmp 与 ve[i] 的叉乘的正负即可
叉乘为正说明 tmp 在 ve[i] 左侧,反之在右侧
最后二分求解
代码:
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 9 const int N = 5010;10 11 struct Point {12 double x, y;13 Point(double x = 0, double y = 0) :x(x), y(y) {}14 }; //点的定义15 16 typedef Point Vector; //向量的定义17 18 Vector operator - (Vector& A, Vector& B) { return Vector(A.x - B.x, A.y - B.y); } //向量减法19 20 double Cross(Vector& A, Vector& B) { return A.x*B.y - A.y*B.x; } //向量叉乘21 22 int cnt[N], X1, Y1, X2, Y2, n, m, id = 0, L[N];23 Vector ve[N];24 25 int calc(Point& p, int l, int r) { //二分26 if (l == r)return l;27 int mid = (l + r) >> 1;28 Vector tmp(p.x - L[mid], p.y - Y2);29 if (Cross(tmp, ve[mid]) < 0)return calc(p, l, mid);30 else return calc(p, mid + 1, r);31 }32 33 int main() {34 int u, l;35 while (cin >> n&&n) {36 memset(cnt, 0, sizeof(cnt));37 scanf("%d%d%d%d%d", &m, &X1, &Y1, &X2, &Y2);38 ve[n].x = 0, ve[n].y = Y1 - Y2;39 L[n] = X1;40 for (int i = 0; i < n; ++i) {41 scanf("%d%d", &u, &l);42 L[i] = l;43 ve[i].x = u - l, ve[i].y = Y1 - Y2;44 }45 Point tmp;46 for (int i = 0; i < m; ++i) {47 scanf("%lf%lf", &tmp.x, &tmp.y);48 ++cnt[calc(tmp, 0, n)];49 }50 if (id++)printf("\n");51 for (int i = 0; i <= n; ++i)52 printf("%d: %d\n", i, cnt[i]);53 }54 }
TOYS
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 15103 | Accepted: 7291 |
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.
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 03 14 36 810 1015 301 52 12 85 540 107 94 10 0 10 100 020 2040 4060 6080 80 5 1015 1025 1035 1045 1055 1065 1075 1085 1095 100
Sample Output
0: 21: 12: 13: 14: 05: 10: 21: 22: 23: 24: 2
Hint
As the example illustrates, toys that fall on the boundary of the box are "in" the box.