POJ 2318 TOYS(计算几何+点与直线位置关系+二分)
2013-10-12 12:34
441 查看
题目链接:http://poj.org/problem?id=2318
求出每个格子里面玩具数量
首先把起始竖线和末尾竖线加上,最后对于每个点二分判断在哪个格子里面,这个二分应该很明显
点在直线哪边可以通过X乘积判断
求出每个格子里面玩具数量
首先把起始竖线和末尾竖线加上,最后对于每个点二分判断在哪个格子里面,这个二分应该很明显
点在直线哪边可以通过X乘积判断
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <cmath> using namespace std; #define maxn 6000 struct point{ double x,y; point(double _x=0,double _y=0):x(_x),y(_y){} }begin,end,temp; struct line{ point a,b; }po[maxn]; int n,m; double cross(point &a,point &b,point &c){ return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y); } bool cmp(const line &a,const line &b){ if(a.b.x != b.b.x) return a.b.x < b.b.x; return a.a.x < a.a.x; } int ans[maxn]; int find_ans(point temp){ if(temp.x>end.x || temp.x <begin.x || temp.y>begin.y || temp.y<end.y) return 0; int l=0,r=n-1,mid; while(l<r){ mid=(l+r)/2; if(l+1==r){ ans[l]++; return 0; } if(cross(temp,po[mid].a,po[mid].b)>0) r=mid; else l=mid; } return 0; } int main(){ int i,j,k; while(scanf("%d",&n),n){ scanf("%d%lf%lf%lf%lf",&m,&begin.x,&begin.y,&end.x,&end.y); po[0].a=point(begin.x,end.y); po[0].b=begin; for(i=1;i<=n;i++){ scanf("%lf%lf",&po[i].b.x,&po[i].a.x); po[i].a.y=end.y,po[i].b.y=begin.y; } n++; po .a=end; po .b=point(end.x,begin.y); n++; sort(po,po+n,cmp); memset(ans,0,sizeof(ans)); for(i=0;i<m;i++){ scanf("%lf%lf",&temp.x,&temp.y); find_ans(temp); } for(i=0;i<n-1;i++) printf("%d: %d\n",i,ans[i]); printf("\n"); } return 0; }
相关文章推荐
- poj 2318 TOYS(计算几何 点跟直线的位置关系)
- POJ 2318 TOYS 计算几何(点与直线关系)
- POJ_2318_TOYS&&POJ_2398_Toy Storage_二分+判断直线和点的位置关系
- poj2318——TOYS(计算几何+点与线段的位置)
- POJ 2318 TOYS + POJ 2398 Toy Storage (点与直线的位置关系)
- POJ 2318 TOYS(计算几何)(二分)
- POJ 2318 TOYS [叉积判断+二分查找]【计算几何】
- 【POJ】2318 TOYS ——计算几何+二分
- poj 2318 toys 计算几何+二分
- POJ 2318 TOYS 计算几何 入门题 叉积 + 二分
- POJ 2318 TOYS (计算几何,向量积,二分查找)
- poj 2318 TOYS(计算几何 点与线段的关系)
- POJ-2318 TOYS 计算几何 判断点在线段的位置
- POJ 2318 TOYS 计算几何 入门题 叉积 + 二分
- poj 2398 Toy Storage(计算几何,点跟直线的位置关系)
- POJ--2318 -- TOYS [点、线基本关系] [计算几何]
- poj 题目2318 TOYS (计算几何,点与边的关系)
- A - TOYS(POJ - 2318) 计算几何的一道基础题
- poj 2318 TOYS (计算几何)
- POJ 2318 TOYS (计算几何,叉积判断)