TOYS(计算几何--点与线的关系(叉积+二分(二分之前必须排序!!!)))
来源:互联网 发布:java初级工程师简历 编辑:程序博客网 时间:2024/06/06 16:55
Link:http://poj.org/problem?id=2318
TOYS
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 12022 Accepted: 5799
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.
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.
Source
Rocky Mountain 2003
题意:一个矩形,有被若干直线分成N个格子,给出一个点的坐标,问你该点位于哪个点中。
编程思想:其实就是点在凸四边形内的判断,若利用叉积的性质,可以二分求解。
AC code1:
#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath>#define LL long long#define MAXN 1000010 using namespace std;struct point{double x;double y;};struct v{point s;point e;}line[MAXN];double multi(point p1,point p2,point p0){return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}int cnt[MAXN];int n,m;point toy[MAXN];void Bsearch(point p){int l=1;int r=n;while(l<r){int mid=(l+r)/2;if(multi(p,line[mid].s,line[mid].e)<0)//on leftr=mid-1;elsel=mid+1;}if(multi(p,line[l].s,line[l].e)<0)//on left{cnt[l-1]++;}else{cnt[l]++;}}int main(){//freopen("D:\in.txt","r",stdin);double x1,y1,x2,y2,Ui,Li;int i,j,cas;cas=0;while(scanf("%d",&n)!=EOF){if(n==0)break;cas++;scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);for(i=1;i<=n;i++){scanf("%lf%lf",&Ui,&Li);line[i].s.x=Ui;line[i].s.y=y1;line[i].e.x=Li;line[i].e.y=y2;}memset(cnt,0,sizeof(cnt));for(j=1;j<=m;j++){scanf("%lf%lf",&toy[j].x,&toy[j].y);Bsearch(toy[j]);}if(cas>1)printf("\n");for(i=0;i<=n;i++){printf("%d: %d\n",i,cnt[i]);}}return 0; }
AC code2:
#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath>#define LL long long#define MAXN 1000010 using namespace std;struct point{double x;double y;};struct v{point s;point e;}line[MAXN];double multi(point p1,point p2,point p0){return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}int cnt[MAXN];int n,m;point toy[MAXN];void Bsearch(point p){int l=0;int r=n+1;while(l<r){int mid=(l+r)/2;if(multi(p,line[mid].s,line[mid].e)<0)//on leftr=mid-1;elsel=mid+1;}if(multi(p,line[l].s,line[l].e)<0)//on left{cnt[l-1]++;}else{cnt[l]++;}}int main(){//freopen("D:\in.txt","r",stdin);double x1,y1,x2,y2,Ui,Li;int i,j,cas;cas=0;while(scanf("%d",&n)!=EOF){if(n==0)break;cas++;scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);line[0].s.x=x1;line[0].s.y=y1;line[0].e.x=x1;line[0].e.y=y2;line[n+1].s.x=x2;line[n+1].s.y=y1;line[n+1].e.x=x2;line[n+1].e.y=y2;for(i=1;i<=n;i++){scanf("%lf%lf",&Ui,&Li);line[i].s.x=Ui;line[i].s.y=y1;line[i].e.x=Li;line[i].e.y=y2;}memset(cnt,0,sizeof(cnt));for(j=1;j<=m;j++){scanf("%lf%lf",&toy[j].x,&toy[j].y);Bsearch(toy[j]);}if(cas>1)printf("\n");for(i=0;i<=n;i++){printf("%d: %d\n",i,cnt[i]);}}return 0; }
下面是该题目的升级版本:
Link:http://poj.org/problem?id=2398
Toy Storage
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 4597 Accepted: 2723
Description
Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza 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 Reza to find his favorite toys anymore.
Reza's parents came up with the following idea. They put cardboard partitions into the box. Even if Reza keeps throwing his toys into the box, at least toys that get thrown into different partitions stay separate. The box looks like this from the top:
We want for each positive integer t, such that there exists a partition with t toys, determine how many partitions have t, toys.
Reza's parents came up with the following idea. They put cardboard partitions into the box. Even if Reza keeps throwing his toys into the box, at least toys that get thrown into different partitions stay separate. The box looks like this from the top:
We want for each positive integer t, such that there exists a partition with t toys, determine how many partitions have t, toys.
Input
The input consists of a number of cases. The first line consists of six integers n, m, x1, y1, x2, y2. The number of cardboards to form the partitions is n (0 < n <= 1000) and the number of toys is given in m (0 < m <= 1000). 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 each consists of two integers Ui Li, indicating that the ends of the ith cardboard is at the coordinates (Ui, y1) and (Li, y2). You may assume that the cardboards do not intersect with each other. The next m lines each consists of two integers Xi Yi specifying where the ith toy has landed in the box. You may assume that no toy will land on a cardboard.
A line consisting of a single 0 terminates the input.
A line consisting of a single 0 terminates the input.
Output
For each box, first provide a header stating "Box" on a line of its own. After that, there will be one line of output per count (t > 0) of toys in a partition. The value t will be followed by a colon and a space, followed the number of partitions containing t toys. Output will be sorted in ascending order of t for each box.
Sample Input
4 10 0 10 100 020 2080 8060 6040 405 1015 1095 1025 1065 1075 1035 1045 1055 1085 105 6 0 10 60 04 315 303 16 810 102 12 81 55 540 107 90
Sample Output
Box2: 5Box1: 42: 1
Source
Tehran 2003 Preliminary
编程思想:现在给的线段不是按从左到右来的,是无序的,需要自己排序,输出也比较难,这里用到了离散化的思想来存储输出数据。
AC code:
#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath>#define LL long long#define MAXN 1000010 using namespace std;struct point{double x;double y;};struct v{point s;point e;}line[MAXN];bool cmp(v line1,v line2){return line1.s.x<line2.s.x;}double multi(point p1,point p2,point p0){return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}int cnt[MAXN];vector<int>vec[MAXN];int n,m;point toy[MAXN];void Bsearch(point p){int l=0;int r=n+1;while(l<r){int mid=(l+r)/2;if(multi(p,line[mid].s,line[mid].e)<0)//on leftr=mid-1;elsel=mid+1;}if(multi(p,line[l].s,line[l].e)<0)//on left{cnt[l-1]++;}else{cnt[l]++;}}int main(){//freopen("D:\in.txt","r",stdin);double x1,y1,x2,y2,Ui,Li;int i,j,cas,t;cas=0;while(scanf("%d",&n)!=EOF){if(n==0)break;cas++;scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);line[0].s.x=x1;line[0].s.y=y1;line[0].e.x=x1;line[0].e.y=y2;line[n+1].s.x=x2;line[n+1].s.y=y1;line[n+1].e.x=x2;line[n+1].e.y=y2;for(i=1;i<=n;i++){scanf("%lf%lf",&Ui,&Li);line[i].s.x=Ui;line[i].s.y=y1;line[i].e.x=Li;line[i].e.y=y2;}memset(cnt,0,sizeof(cnt));sort(line,line+n+2,cmp);for(j=1;j<=m;j++){scanf("%lf%lf",&toy[j].x,&toy[j].y);Bsearch(toy[j]);}/*if(cas>1)printf("\n");*/for(i=0;i<=n;i++)vec[i].clear();for(i=0;i<=n;i++){vec[cnt[i]].push_back(i);}printf("Box\n");for(t=1;t<=m;t++){int size=vec[t].size();if(size>0){printf("%d: %d\n",t,size);}}}return 0; }
0 0
- TOYS(计算几何--点与线的关系(叉积+二分(二分之前必须排序!!!)))
- POJ 2318 TOYS(计算几何+点与直线位置关系+二分)
- poj2318 TOYS(计算几何+叉积+二分)
- [POJ2318]TOYS(计算几何+二分)
- [POJ2318]TOYS(计算几何+二分)
- poj2318—TOYS(计算几何+二分)
- POJ 2318 TOYS 计算几何(点与直线关系)
- poj 题目2318 TOYS (计算几何,点与边的关系)
- 几何+点与线段的位置关系+二分(POJ2318)
- POJ 2318 TOYS(计算几何)(二分)
- poj 2318 TOYS(计算几何 点跟直线的位置关系)
- poj2318 TOYS 【计算几何】【点和线的关系】
- poj2318——TOYS(计算几何+点与线段的位置)
- POJ 2318 TOYS 计算几何 入门题 叉积 + 二分
- POJ2318-toys(计算几何基础+二分)
- poj 2318 toys 计算几何+二分
- Poj 2318 toys (计算几何,叉积)
- POJ2318 TOYS(叉积,计算几何)
- Mac地址加密
- CentOS 7下安装MongoDB
- [leetcode]Copy List with Random Pointer
- ubuntu 安装vmware tools
- 使用教程
- TOYS(计算几何--点与线的关系(叉积+二分(二分之前必须排序!!!)))
- Linux(Ubuntu12)上安装Appium
- HDU 5831(思路题目)
- Android面试准备
- 两个viewport的故事(第二部分)
- hdu1875 畅通工程再续 并查集+kruskal最小生成树
- BZOJ1009
- 图表轴刻度的思路,算法: 转载4【Excel是如何自动计算坐标轴刻度】
- 解决必须要alert一下,才生效的问题