给出一个矩形盒子,然后把他分成几部分,然后给出几个玩具的坐标,问最后每个部分里的玩具数量是多少输出...
可以用二分来判断玩具落在哪个部分里,然后用叉积来判断在线段的左边还是右边
#include <queue>
#include <stack>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int MAX = 5010;
struct point{
int x,y;
};
struct SEG{
point u,l;
};
SEG s[MAX];
point toy[MAX];
int sum[MAX];
int crossProduct(point a,point b,point c)//向量 ac 在 ab 的方向
{
return (c.x - a.x)*(b.y - a.y) - (b.x - a.x)*(c.y - a.y);
}
int main()
{
int n,m,x1,y1,x2,y2,a,b;
while( ~scanf("%d",&n) && n )
{
memset(sum,0,sizeof(sum));
scanf("%d %d %d %d %d",&m,&x1,&y1,&x2,&y2);
s[0].u.x = x1; s[0].u.y = y1;
s[0].l.x = x1; s[0].l.y = y2;
for(int i=1; i<=n; i++)
{
scanf("%d %d",&a,&b);
s[i].u.x = a; s[i].u.y = y1;
s[i].l.x = b; s[i].l.y = y2;
}
n++;
s[n].u.x = x2; s[n].u.y = y1;
s[n].l.x = x2; s[n].l.y = y2;
for(int i=0; i<m; i++)
{
scanf("%d %d",&toy[i].x,&toy[i].y);
int l=0,r=n,mid;
while(1)
{
mid= (l+r)>>1;
if(crossProduct(s[mid].u,toy[i],s[mid].l)>0)
l=mid;
else r=mid;
if(l+1==r)break;
}
sum[l]++;
}
for(int i=0; i<n; i++)
printf("%d: %d\n",i,sum[i]);
printf("\n");
}
return 0;
}

浙公网安备 33010602011771号