【解题报告】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 有一个长方形,用线段划分若干区域,给若干个点,问每个区域点的分布情况 分析:点和线段的位置判断可以用叉积判断.给的线段是排好序的,但是点是无序的,所以可以用二分优化 ...
随机推荐
- PHP函数中默认参数的的写法
函数可以定义 C++ 风格的标量参数默认值,如下所示: Example #3 在函数中使用默认参数 <?php function makecoffee($type = "cappucc ...
- 传说中的WCF(3):多个协定
我们知道,WCF服务端是先定义服务协定,其实就是一个接口,然后通过实现接口来定义服务类.那么,有一个问题,如果一个服务类同时实现N个接口(也就是有N个协定)呢?结果会如何? 不必猜,我们还是通过实验来 ...
- OpenGL程序运行提示glut32.dll丢失问题
转: http://blog.csdn.net/liufeng520/article/details/8064170 今天调试OpenGl的源程序,编译通过,但一运行就提示,计算机丢失 glut32. ...
- MongoDB 管理工具:Robomongo
http://www.open-open.com/lib/view/open1383029577546.html
- 540A: Combination Lock
题目链接:http://codeforces.com/problemset/problem/540/A 题意: 输入的两个长度一样的数,求对应位置的某位数到下一个数需要最小的步长,每次只能先前或先后走 ...
- JSTL Tag学习笔记(一)之<c: />
注:本文中的例子主要来自http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm. ======================= ...
- Java-马士兵设计模式学习笔记-命令模式
一.概述 命令模式 二.代码 1.Client.java public class Client { public void request(Server server){ server.addCom ...
- C++:四种必须使用初始化列表情况
[c++]必须在类初始化列表中初始化的几种情况 1. 类成员为const类型 2. 类成员为引用类型 复制代码 #include <iostream> using namesp ...
- Android实现透明的颜色效果
android Button或者ImageButton背景透明状态设置 设置Button或ImageButton的背景为透明或者半透明 半透明< Button android:backgroun ...
- TEET
[{"PROCESS_STORE_TIME":"3min 11s","PROCESS_GET_FILE_TIME":"3min&q ...