POJ 2318 - TOYS - [计算几何基础题]
题目链接:http://poj.org/problem?id=2318
Time Limit: 2000MS Memory Limit: 65536K
Description
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
Output
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
题意:
给出n条“分割纸板”,将一个长方形纸盒分割成n+1个区间,编号为0~n;
给出m个玩具(每个玩具一个坐标,且必然严格落在区间内),问各个区域有多少个点。
题解:
加上纸盒左右两个边界,总共n+2条直线;
枚举每个玩具的坐标,二分判断在哪两条直线中间(由于题目给出直线的时候是按照从左往右的,所以不需要排序),这个区间的cnt+=1;
AC代码:
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<vector>
- #define MAX 5005
- #define M_PI 3.14159265358979323846 //POJ的math头文件好像没有这个定义
- using namespace std;
- //--------------------------------------计算几何模板 - st--------------------------------------
- const double eps = 1e-;
- struct Point{
- double x,y;
- Point(double tx=,double ty=):x(tx),y(ty){}
- };
- typedef Point Vctor;
- //向量的加减乘除
- Vctor operator + (Vctor A,Vctor B){return Vctor(A.x+B.x,A.y+B.y);}
- Vctor operator - (Point A,Point B){return Vctor(A.x-B.x,A.y-B.y);}
- Vctor operator * (Vctor A,double p){return Vctor(A.x*p,A.y*p);}
- Vctor operator / (Vctor A,double p){return Vctor(A.x/p,A.y/p);}
- bool operator < (Point A,Point B){return A.x < B.x || (A.x == B.x && A.y < B.y);}
- struct Line{
- Point p;
- Vctor v;
- Line(Point p=Point(0,0),Vctor v=Vctor(,)):p(p),v(v){}
- Point point(double t){return p + v*t;} //获得直线上的距离p点t个单位长度的点
- };
- struct Circle{
- Point c;
- double r;
- Circle(Point tc=Point(,),double tr=):c(tc),r(tr){}
- Point point(double a){return Point( c.x + cos(a)*r , c.y + sin(a)*r);}
- };
- int dcmp(double x)
- {
- if(fabs(x)<eps) return ;
- else return (x<)?(-):();
- }
- bool operator == (Point A,Point B){return dcmp(A.x-B.x)== && dcmp(A.y-B.y)==;}
- //向量的点积,长度,夹角
- double Dot(Vctor A,Vctor B){return A.x*B.x+A.y*B.y;}
- double Length(Vctor A){return sqrt(Dot(A,A));}
- double Angle(Vctor A,Vctor B){return acos(Dot(A,B)/Length(A)/Length(B));}
- //叉积,三角形面积
- double Cross(Vctor A,Vctor B){return A.x*B.y-A.y*B.x;}
- double TriangleArea(Point A,Point B,Point C){return Cross(B-A,C-A);}
- //向量的旋转,求向量的单位法线(即左转90度,然后长度归一)
- Vctor Rotate(Vctor A,double rad){return Vctor( A.x*cos(rad) - A.y*sin(rad) , A.x*sin(rad) + A.y*cos(rad) );}
- Vctor Normal(Vctor A)
- {
- double L = Length(A);
- return Vctor(-A.y/L, A.x/L);
- }
- //直线的交点
- Point getLineIntersection(Line L1,Line L2)
- {
- Vctor u = L1.p-L2.p;
- double t = Cross(L2.v,u)/Cross(L1.v,L2.v);
- return L1.p + L1.v*t;
- }
- //点到直线的距离
- double DistanceToLine(Point P,Line L)
- {
- return fabs(Cross(P-L.p,L.v))/Length(L.v);
- }
- //点到线段的距离
- double DistanceToSegment(Point P,Point A,Point B)
- {
- if(A==B) return Length(P-A);
- Vctor v1 = B-A, v2 = P-A, v3 = P-B;
- if (dcmp(Dot(v1,v2)) < ) return Length(v2);
- else if (dcmp(Dot(v1,v3)) > ) return Length(v3);
- else return fabs(Cross(v1,v2))/Length(v1);
- }
- //点到直线的映射
- Point getLineProjection(Point P,Line L)
- {
- return L.v + L.v*Dot(L.v,P-L.p)/Dot(L.v,L.v);
- }
- //判断线段是否规范相交
- bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)
- {
- double c1 = Cross(a2 - a1,b1 - a1), c2 = Cross(a2 - a1,b2 - a1),
- c3 = Cross(b2 - b1,a1 - b1), c4 = Cross(b2 - b1,a2 - b1);
- return dcmp(c1)*dcmp(c2)< && dcmp(c3)*dcmp(c4)<;
- }
- //判断点是否在一条线段上
- bool OnSegment(Point P,Point a1,Point a2)
- {
- return dcmp(Cross(a1 - P,a2 - P))== && dcmp(Dot(a1 - P,a2 - P))<;
- }
- //多边形面积
- double PolgonArea(Point *p,int n)
- {
- double area=;
- for(int i=;i<n-;i++) area += Cross( p[i]-p[] , p[i + ]-p[] );
- return area/;
- }
- //判断圆与直线是否相交以及求出交点
- int getLineCircleIntersection(Line L,Circle C,vector<Point> &sol)
- {
- double t1,t2;
- double a = L.v.x, b = L.p.x - C.c.x, c = L.v.y, d = L.p.y - C.c.y;
- double e = a*a + c*c , f = *(a*b + c*d), g = b*b + d*d - C.r*C.r;
- double delta = f*f - 4.0*e*g;
- if(dcmp(delta)<) return ;
- else if(dcmp(delta)==)
- {
- t1 = t2 = -f/(2.0*e);
- sol.push_back(L.point(t1));
- return ;
- }
- else
- {
- t1 = (-f-sqrt(delta))/(2.0*e); sol.push_back(L.point(t1));
- t2 = (-f+sqrt(delta))/(2.0*e); sol.push_back(L.point(t2));
- return ;
- }
- }
- //判断并求出两圆的交点
- double angle(Vctor v){return atan2(v.y,v.x);}
- int getCircleIntersection(Circle C1,Circle C2,vector<Point> &sol)
- {
- double d = Length(C1.c - C2.c);
- //圆心重合
- if(dcmp(d)==)
- {
- if(dcmp(C1.r-C2.r)==) return -; //两圆重合
- else return ; //包含关系
- }
- //圆心不重合
- if(dcmp(C1.r+C2.r-d)<) return ; // 相离
- if(dcmp(fabs(C1.r-C2.r)-d)>) return ; // 包含
- double a = angle(C2.c - C1.c);
- double da = acos((C1.r*C1.r + d*d - C2.r*C2.r) / (*C1.r*d));
- Point p1 = C1.point(a - da), p2 = C1.point(a + da);
- sol.push_back(p1);
- if(p1==p2) return ;
- sol.push_back(p2);
- return ;
- }
- //求点到圆的切线
- int getTangents(Point p,Circle C,vector<Line> &sol)
- {
- Vctor u=C.c-p;
- double dis=Length(u);
- if(dis<C.r) return ;
- else if(dcmp(dis-C.r) == )
- {
- sol.push_back(Line(p,Rotate(u,M_PI/)));
- return ;
- }
- else
- {
- double ang=asin(C.r/dis);
- sol.push_back(Line(p,Rotate(u,-ang)));
- sol.push_back(Line(p,Rotate(u,ang)));
- return ;
- }
- }
- //求两圆的切线
- int getCircleTangents(Circle A,Circle B,Point *a,Point *b)
- {
- int cnt = ;
- if(A.r<B.r){swap(A,B);swap(a,b);}
- //圆心距的平方
- double d2 = (A.c.x - B.c.x)*(A.c.x - B.c.x) + (A.c.y - B.c.y)*(A.c.y - B.c.y);
- double rdiff = A.r - B.r;
- double rsum = A.r + B.r;
- double base = angle(B.c - A.c);
- //重合有无限多条
- if(d2 == && dcmp(A.r - B.r) == ) return -;
- //内切
- if(dcmp(d2-rdiff*rdiff) == )
- {
- a[cnt] = A.point(base);
- b[cnt] = B.point(base);
- cnt++;
- return ;
- }
- //有外公切线
- double ang = acos((A.r - B.r) / sqrt(d2));
- a[cnt] = A.point(base + ang); b[cnt] = B.point(base + ang); cnt++;
- a[cnt] = A.point(base - ang); b[cnt] = B.point(base - ang); cnt++;
- //一条内切线
- if(dcmp(d2-rsum*rsum) == )
- {
- a[cnt] = A.point(base);
- b[cnt] = B.point(M_PI + base);
- cnt++;
- }//两条内切线
- else if(dcmp(d2-rsum*rsum) > )
- {
- double ang = acos((A.r + B.r) / sqrt(d2));
- a[cnt] = A.point(base + ang); b[cnt] = B.point(base + ang); cnt++;
- a[cnt] = A.point(base - ang); b[cnt] = B.point(base - ang); cnt++;
- }
- return cnt;
- }
- //--------------------------------------计算几何模板 - ed--------------------------------------
- int n,m;
- Point upper_left,lower_right;
- Line x_axis;//下边界
- Line cardboard[MAX];//隔板
- Point toy;
- struct Area{
- int id,cnt;
- }area[MAX];
- int Left_of_Line(Line l,Point p)
- {
- if(Cross(l.v,p-l.p)>) return ;//左边
- else return ;
- }
- int where(Point toy)
- {
- int l=,r=n+;
- while(r-l>)
- {
- int mid=(l+r)/;
- if(Left_of_Line(cardboard[mid],toy)) r=mid;
- else l=mid;
- }
- return l;
- }
- int main()
- {
- while(scanf("%d",&n) && n!=)
- {
- scanf("%d%lf%lf%lf%lf",&m,&upper_left.x,&upper_left.y,&lower_right.x,&lower_right.y);
- x_axis=Line(lower_right,Vctor(-,));
- cardboard[]=Line(Point(upper_left.x,lower_right.y),Vctor(,)), cardboard[n+]=Line(lower_right,Vctor(,));
- Point U=Point(,upper_left.y),L=Point(,lower_right.y);
- for(int i=;i<=n;i++)
- {
- scanf("%lf%lf",&U.x,&L.x);
- //printf("(%lf,%lf) (%lf,%lf)\n",U.x,U.y,L.x,L.y);
- cardboard[i]=Line(L,U-L);
- //printf("Line %d: p=(%lf,%lf) v=(%lf,%lf)\n",i,cardboard[i].p.x,cardboard[i].p.y,cardboard[i].v.x,cardboard[i].v.y);
- }
- for(int i=;i<=n;i++) area[i]=(Area){i,};
- for(int i=;i<=m;i++)
- {
- scanf("%lf%lf",&toy.x,&toy.y);
- area[where(toy)].cnt++;
- }
- for(int i=;i<=n;i++) printf("%d: %d\n",area[i].id,area[i].cnt);
- printf("\n");
- }
- }
POJ 2318 - TOYS - [计算几何基础题]的更多相关文章
- B - Toy Storage(POJ - 2398) 计算几何基础题,比TOYS多了个线段排序
Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing ...
- POJ 2398 - Toy Storage - [计算几何基础题][同POJ2318]
题目链接:http://poj.org/problem?id=2398 Time Limit: 1000MS Memory Limit: 65536K Description Mom and dad ...
- POJ 2318 TOYS(叉积+二分)
题目传送门:POJ 2318 TOYS Description Calculate the number of toys that land in each bin of a partitioned ...
- 向量的叉积 POJ 2318 TOYS & POJ 2398 Toy Storage
POJ 2318: 题目大意:给定一个盒子的左上角和右下角坐标,然后给n条线,可以将盒子分成n+1个部分,再给m个点,问每个区域内有多少各点 这个题用到关键的一步就是向量的叉积,假设一个点m在 由ab ...
- POJ 2318 TOYS【叉积+二分】
今天开始学习计算几何,百度了两篇文章,与君共勉! 计算几何入门题推荐 计算几何基础知识 题意:有一个盒子,被n块木板分成n+1个区域,每个木板从左到右出现,并且不交叉. 有m个玩具(可以看成点)放在这 ...
- 简单几何(点与线段的位置) POJ 2318 TOYS && POJ 2398 Toy Storage
题目传送门 题意:POJ 2318 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况 分析:点和线段的位置判断可以用叉积判断.给的线段是排好序的,但是点是无序的,所以可以用二分优化 ...
- poj 2318 TOYS (二分+叉积)
http://poj.org/problem?id=2318 TOYS Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 101 ...
- POJ 2318 TOYS && POJ 2398 Toy Storage(几何)
2318 TOYS 2398 Toy Storage 题意 : 给你n块板的坐标,m个玩具的具体坐标,2318中板是有序的,而2398无序需要自己排序,2318要求输出的是每个区间内的玩具数,而231 ...
- poj 2318 TOYS & poj 2398 Toy Storage (叉积)
链接:poj 2318 题意:有一个矩形盒子,盒子里有一些木块线段.而且这些线段坐标是依照顺序给出的. 有n条线段,把盒子分层了n+1个区域,然后有m个玩具.这m个玩具的坐标是已知的,问最后每一个区域 ...
随机推荐
- android中sharedPreferences的用法(转)
SharedPreferences介绍: 做软件开发应该都知道,很多软件会有配置文件,里面存放这程序运行当中的各个属性值,由于其配置信息并不多,如果采用数据库来存放并不划算,因为数据库连接跟操作等 ...
- Java NIO原理 图文分析及代码实现
Java NIO原理图文分析及代码实现 前言: 最近在分析hadoop的RPC(Remote Procedure Call Protocol ,远程过程调用协议,它是一种通过网络从远程计算机程序上请 ...
- Linux wget 命令下载文件
wget是Linux系统中用来下载文件的工具,其功能还是比较多的,能够下载单个文件,也可以分段下载,下面小编将针对wget命令的用法给大家做个实例介绍. 实例1 :下载单个文件 # wget http ...
- 【RF库Collections测试】Keep In Dictionary
Name:Keep In DictionarySource:Collections <test library>Arguments:[ dictionary | *keys ]Keeps ...
- python --->字典 集合 学习笔记
1.字典--->创建空字典:dict={} broa=["李宁",”耐克“,“阿迪达斯”,“鱼c工作室”] sloga=[“A”,“B”,“C”,“D”] dict={&qu ...
- linux实现开机自启动脚本
Linux下(以RedHat为范本)添加开机自启动脚本有两种方法,先来简单的; 一.在/etc/rc.local中添加如果不想将脚本粘来粘去,或创建链接什么的,则:step1. 先修改好脚本,使其所有 ...
- N76E003之IIC
I2C 总线提供了一种串行通信方式,用在 MCU 与 EEPROM,LCD模块,温度传感器等等之间控制.I2C 用两条线 (数据线SDA 和时钟线 SCL) 在设备间传输数据.I2C 总线用作主机与从 ...
- 非static成员函数通过类名::来调用,空指针调用成员方法不出错!
首先来看这一段代码: #include <iostream> using namespace std; class A{ public: int k; void p1(){ cout< ...
- XML读取(string形式进行读取)
#region 测试XML二进制读取 string strXmlData = "<xml><ToUserName><![CDATA[gh_ef65912f88f ...
- LeetCode 43 Multiply Strings(字符串相乘)
题目链接: https://leetcode.com/problems/multiply-strings/?tab=Description 求解大数相乘问题 按照上图所示,进行嵌套循环计算 ...