您的位置:首页 > 其它

POJ 2318 TOYS(计算几何+点与直线位置关系+二分)

2013-10-12 12:34 441 查看
题目链接:http://poj.org/problem?id=2318

求出每个格子里面玩具数量

首先把起始竖线和末尾竖线加上,最后对于每个点二分判断在哪个格子里面,这个二分应该很明显

点在直线哪边可以通过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