首页 > 代码库 > poj2318(计算几何)
poj2318(计算几何)
1.题目(theme)
TOYS
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 10488 | Accepted: 5031 |
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.
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.
2.题目大意(The meaning of the questions)
一个格子放置玩具,用隔板分开,输入隔板数n,玩具数m,格子的左上角和右下角的坐标x1,y1,x2,y2(用来表示格子大小),然后输入n个隔板的位置ui和Li,坐标为(上面(ui,y1),下面(Li,y2)),m个玩具的坐标x和y;
3.解题方法(solution)
1.找到一个i,使得Li>x;然后用叉积判断在那边,然后决定向左还是向右移,直到边界条件。
1 #include <iostream> 2 #include <cmath> 3 #include <string.h> 4 #include <cstdio> 5 using namespace std; 6 int a[5005],b[5005],c[5005]; 7 int main() 8 { 9 int n,m,x1,y1,x2,y2,i,j,k,x,y,l1,l2,l3,l4;10 while (scanf("%d%d%d%d%d%d",&n,&m,&x1,&y1,&x2,&y2)==6){11 for (i=0;i<5005;i++){12 c[i]=0;13 }14 b[n]=x2;a[n]=y2;15 for (i=0;i<n;i++){16 scanf("%d%d",&a[i],&b[i]);17 }18 for (i=0;i<m;i++){19 scanf("%d%d",&x,&y);20 j=0;21 while (b[j]<x&&j<n){22 j++;23 }24 l1=a[j]-x;25 l2=y1-y;26 l3=b[j]-x;27 l4=y2-y;28 if (l1*l4-l2*l3>0){29 while (l1*l4-l2*l3>0&&j<n){30 j++;31 l1=a[j]-x;32 l2=y1-y;33 l3=b[j]-x;34 l4=y2-y;35 }36 c[j]++;37 }else {38 while (l1*l4-l2*l3<0){39 j--;40 l1=a[j]-x;41 l2=y1-y;42 l3=b[j]-x;43 l4=y2-y;44 }45 c[j+1]++;46 }47 }48 for (i=0;i<=n;i++){49 printf("%d: %d\n",i,c[i]);50 }51 printf("\n");52 }53 return 0;54 }
2.二分加叉积:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。