poj 2318 TOY题解
来源:互联网 发布:linux 文件所有者 编辑:程序博客网 时间:2024/05/22 03:12
二分+叉积
如图所示:N个隔板将盒子分为N+1个区域,若toy在盒子隔板的左边则叉积小于0,在右边则叉积大于0。
为此,则可以用二分查找确定该点位于哪个区域(当l==r时,根据在隔板的那一侧,便可确定在哪个区域了)。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAX 5005
using namespace std;
int N,M;
double x1,y1,x2,y2;
typedef struct{
double x;
double y;
}P;
P toy[MAX];
struct Node{
P first;
P second;
}Pline[MAX];
int pnum[MAX];
double mul(P p1,P p2,P p0){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
void bin(P tmp){
int l=0,r=N-1;
while(l<r){
int mid=(l+r)/2;
if(mul(tmp,Pline[mid].first,Pline[mid].second)>0){
l=mid+1;
}else{
r=mid;
}
}
if(mul(tmp,Pline[l].first,Pline[l].second)>0) pnum[l+1]++;
else pnum[l]++;
}
int main(){
while(~scanf("%d",&N),N){
memset(pnum,0,sizeof(pnum));
scanf("%d %lf %lf %lf %lf",&M,&x1,&y1,&x2,&y2);
for(int i=0;i<N;i++){
scanf("%lf %lf",&Pline[i].first.x,&Pline[i].second.x);
Pline[i].first.y=y1;
Pline[i].second.y=y2;
}
for(int i=0;i<M;i++){
scanf("%lf %lf",&toy[i].x,&toy[i].y);
bin(toy[i]);
}
for(int i=0;i<=N;i++){
printf("%d: %d\n",i,pnum[i]);
}
puts("");
}
return 0;
}
- poj 2318 TOY题解
- POJ 2318 TOYS && POJ 2398 Toy Storage
- POJ 2318 TOYS || POJ 2398 Toy Storage
- 7_4_P题 Toy Storage 题解[Poj 2398](计算几何,点线关系,二分)
- poj 2398 Toy Storage --基本上和poj 2318 一样
- poj 2318 TOYS & poj 2398 Toy Storage (叉积)
- 【叉积性质】POJ 2318 TOYS && POJ 2398 Toy Storage
- POJ 2318:TOYS & POJ 2398:Toy Storage 计算几何
- HNOI2008玩具装箱toy题解
- poj 2398 Toy Storage
- poj 2398 Toy Storage
- POJ 2398 Toy Storage
- POJ 2398 Toy Storage
- poj 2398 toy storage
- poj 2398 Toy Storage
- POJ 2398 Toy Storage
- poj 2398Toy Storage
- POJ 2398 Toy Storage
- mx:DataGrid与s:DataGrid的写法
- 因子和阶乘
- android中通过Application设置全局变量
- ORACLE中常见SET指令
- bug 处处有
- poj 2318 TOY题解
- 挺好的一个关于at new/ at end of的说明
- 摩罗摩嘎
- 简洁清爽全屏自适应Jquery幻灯片
- ios学习之 关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系
- HDU 3790 最短路
- 《0bug-C/C++商用工程之道》节选01--内存栈-1
- Tomcat如何与Servlet对象进行交互
- 图论模板整理