【解题报告】PKU 2318 TOYS AND PKU 2398 Toy Storage
题目连接: http://poj.org/problem?id=2318 http://poj.org/problem?id=2398
两题类似的题目,2398是2318的升级版。
题目大概是说,有一个矩形的柜子,中间有一些隔板。告诉你每个隔板的坐标,还有一些玩具的坐标,统计玩具在哪个格子里。
这题的思路很简单,如果玩具在某个隔板的左边和右边叉乘的正负是不同的。如图:
图中点P在线段CD的左边,则向量PC和向量PD叉乘结果小于0。反之P在AB的左边,向量PA叉乘PB大于0。
因此利用这个性质以及排序好的线段列表,通过二分思想,可以快速知道点在哪个格子内。
代码:
- #include<stdio.h>
- #include<math.h>
- #define PI 3.14159265358979323846
- #define MAX(x,y) ((x)>(y)?(x):(y))
- #define MIN(x,y) ((x)<(y)?(x):(y))
- #define ABS(x) (((x)>0)?(x):(-(x)))
- #define SIGN(x) (((x)<0)?-1:1)
- #define EPS 0.000001/*精度控制*/
- #define N 5005
- /*坐标的定义*/
- typedef double coo;/*int*/
- /*点、向量*/
- typedef struct POINT
- {
- coo x,y;
- }point,vector;
- /*线段*/
- typedef struct SEGMENT
- {
- point p1,p2;/*p[2];*/
- }segment;
- /*判相等*/
- int is_equel(double a,double b)
- {
- double c=ABS(a-b);
- if(c<=EPS) return ;/*相等*/
- else return ;/*不相等*/
- }
- /*向量的减法p1-p2*/
- vector vector_minus(vector p1,vector p2)
- {
- vector p;
- p.x=p1.x-p2.x;
- p.y=p1.y-p2.y;
- return p;
- }
- /*向量叉乘*/
- double cross_product(vector p1,vector p2)
- {/*x1y2-x2y1*/
- return p1.x*p2.y-p1.y*p2.x;
- }
- int bijiao(segment s1,segment s2)/*比较两个线段的位置< */
- {
- if(s1.p1.x<s2.p1.x) return ;
- if(s1.p1.x==s2.p1.x&&s1.p2.x<s2.p2.x) return ;
- return ;
- }
- int Partition(segment r[],int low,int high)/*升序*/
- /*返回支点最终位置*/
- {
- segment x;/*类型具体*/
- if(low>high) return ;
- if(low==high) return low;
- x=r[low];
- while(low<high)
- {
- while(low<high&&bijiao(x,r[high])) high--; /*<*/
- if(low<high){r[low]=r[high];low++;}
- while(low<high&&bijiao(r[low],x)) low++; /*>*/
- if(low<high){r[high]=r[low];high--;}
- }
- r[low]=x;
- return low;
- }
- void Quick_sort(segment r[],int m,int n) /*排序从r[m]到r[n]*/
- {
- int i;
- if(m>=n) return;
- i=Partition(r,m,n);
- Quick_sort(r,m,i-);
- Quick_sort(r,i+,n);
- }
- int BinSearch(segment a[],int n,point k)/*在有序的数组a[0]~a[n-1]中查找k元素*/
- {
- int low=,high=n-,mid;
- while(low<=high)
- {
- mid=low+((high-low)/);
- if(cross_product(vector_minus(a[mid].p1,k),vector_minus(a[mid].p2,k))<) high=mid-;/*线段在右边*/
- else low=mid+;
- }
- if(low>high) return high;
- }
- int main()
- {
- int n,m,i,a[N],b[N];
- double x1,x2,y1,y2;
- point p;
- segment s[N];
- while()
- {
- scanf("%d",&n);
- if(n==) break;
- scanf("%d%lf%lf%lf%lf",&m,&x1,&y1,&x2,&y2);
- s[].p1.x=x1;
- s[].p1.y=y1;
- s[].p2.x=x1;
- s[].p2.y=y2;
- for(i=;i<=n;i++)
- {
- scanf("%lf%lf",&s[i].p1.x,&s[i].p2.x);
- s[i].p1.y=y1;
- s[i].p2.y=y2;
- }
- s[i].p1.x=x2;
- s[i].p1.y=y1;
- s[i].p2.x=x2;
- s[i].p2.y=y2;
- Quick_sort(s,,n+);
- for(i=;i<N;i++) a[i]=;
- for(i=;i<m;i++)
- {
- scanf("%lf%lf",&p.x,&p.y);
- a[BinSearch(s,n+,p)]++;
- }
- for(i=;i<N;i++) b[i]=;
- for(i=;i<N;i++) b[a[i]]++;
- printf("Box\n");
- for(i=;i<N;i++) if(b[i]) printf("%d: %d\n",i,b[i]);
- }
- return ;
- }
PKU 2398
【解题报告】PKU 2318 TOYS AND PKU 2398 Toy Storage的更多相关文章
- poj 2318 TOYS & poj 2398 Toy Storage (叉积)
链接:poj 2318 题意:有一个矩形盒子,盒子里有一些木块线段.而且这些线段坐标是依照顺序给出的. 有n条线段,把盒子分层了n+1个区域,然后有m个玩具.这m个玩具的坐标是已知的,问最后每一个区域 ...
- POJ 2318 TOYS && POJ 2398 Toy Storage(几何)
2318 TOYS 2398 Toy Storage 题意 : 给你n块板的坐标,m个玩具的具体坐标,2318中板是有序的,而2398无序需要自己排序,2318要求输出的是每个区间内的玩具数,而231 ...
- poj 2398 Toy Storage(计算几何)
题目传送门:poj 2398 Toy Storage 题目大意:一个长方形的箱子,里面有一些隔板,每一个隔板都可以纵切这个箱子.隔板将这个箱子分成了一些隔间.向其中扔一些玩具,每个玩具有一个坐标,求有 ...
- POJ 2398 - Toy Storage 点与直线位置关系
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5439 Accepted: 3234 Descr ...
- POJ 2398 Toy Storage(计算几何,叉积判断点和线段的关系)
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3146 Accepted: 1798 Descr ...
- poj 2398 Toy Storage(计算几何 点线关系)
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4588 Accepted: 2718 Descr ...
- 2018.07.04 POJ 2398 Toy Storage(二分+简单计算几何)
Toy Storage Time Limit: 1000MS Memory Limit: 65536K Description Mom and dad have a problem: their ch ...
- POJ 2398 Toy Storage (叉积判断点和线段的关系)
题目链接 Toy Storage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4104 Accepted: 2433 ...
- 简单几何(点与线段的位置) POJ 2318 TOYS && POJ 2398 Toy Storage
题目传送门 题意:POJ 2318 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况 分析:点和线段的位置判断可以用叉积判断.给的线段是排好序的,但是点是无序的,所以可以用二分优化 ...
随机推荐
- ExtJs之VTYPE验证
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- Linux操作系统下的Sudo命令
查看.修改或者执行某些命令需要root用户的权限,如果不想直接切换到root用户,就可以使用sudo命令.sudo命令用于针对单个命令授予临时权限.sudo仅在需要时授予用户权限,减少了用户因为错误执 ...
- 【hdu2815-Mod Tree】高次同余方程-拓展BadyStepGaintStep
http://acm.hdu.edu.cn/showproblem.php?pid=2815 题意:裸题... 关于拓展BSGS的详细解释我写了一篇博文:http://www.cnblogs.com/ ...
- Project Euler 109 :Darts 飞镖
Darts In the game of darts a player throws three darts at a target board which is split into twenty ...
- Spring笔记——使用Spring进行面向切面(AOP)编程
要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间: =================== Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种: 1. 基于XM ...
- http://blog.csdn.net/hguisu/article/details/7533759
http://blog.csdn.net/hguisu/article/details/7533759
- Android 使用全局变量传递数据
使用全局变量传递数据,所谓的全局变量类似于jee开发中的application变量.申明后,全局调用.只有当内存被清理后,才被销毁.否则一直可以调用. 还是使用点击一个button,传递一个数据到另一 ...
- SQL SERVER ->> Data Compression
最近做了一个关于数据压缩的项目,要把整个SQL SERVER服务器下所有的表对象要改成页压缩.于是趁此机会了解了一下SQL SERVER下压缩技术. 这篇文章几乎就是完全指导手册了 https://t ...
- json 得到时分秒为00:00:00,拿不到时分秒 解决办法
数据库查询时间没有了时分秒的解决办法 问题出处,公司一个项目中使用动态sql方式查询Oracle数据库,在展示时Date类型字段只展示日期,无时分秒. 分析: ...
- Win API 内存整理
记得我的笔记本上曾经安装了一款名为内存整理大师的软件,当时觉得挺好用而且挺NB的,就是导致开机启动有点慢. 当时我就在想,内存整理是怎么实现的?不过那是水平实在是不怎么样,估计连windows程序的消 ...