题目链接
题目大意
有一个长方形的盒子,盒子里面有n个分割线,告诉你分割线的位置,和m个玩具的位置,问你每个小分割盒子里的玩具数量
解题思路
我们现在已知分割线的位置和玩具的位置,我们只要遍历分割线的位置,然后判断玩具是否在两个相邻分割线的左边和右边就可以了
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=1e5+5;
struct node
{
double x,y;
node() {}
node(double X,double Y)
{
x=X;
y=Y;
}
node operator - (const node &a)
{
return node(x-a.x,y-a.y);
}
};
node e[N];
struct line
{
node x,y;
line() {}
line(node X,node Y)
{
x=X;
y=Y;
}
};
line l[N];
int n,m,ans[N];
double cross(node a,node b)
{
return a.x*b.y-a.y*b.x;
}
int right(line a,node b)
{
if(cross((a.y-a.x),(b-a.x))<0.0)
return 1;
else if(cross((a.y-a.x),(b-a.x))>0.0)
return 0;
return -1;
}
int main()
{
double x1,x2,y1,y2,ui,li,x,y;
int flag=0;
while(~scanf("%d",&n))
{
if(n==0)
break;
flag=1;
memset(ans,0,sizeof(ans));
scanf("%d",&m);
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
for(int i=1; i<=n; i++)
{
scanf("%lf %lf",&ui,&li);
l[i]=line(node(ui,y1),node(li,y2));
}
l[0]=line(node(x1,y1),node(x1,y2));
l[++n]=line(node(x2,y1),node(x2,y2));
for(int h=1; h<=m; h++)
{
scanf("%lf %lf",&x,&y);
if(x<x1||x>x2||y<y2||y>y1)//出界不算
continue;
for(int i=0; i<n; i++)
{
if((right(l[i],node(x,y))==0)&&(right(l[i+1],node(x,y))==1))
//在第i条线的右边和第i+1条线的左边
{
ans[i]++;
break;
}
}
}
for(int i=0; i<n; i++)
printf("%d: %d\n",i,ans[i]);
printf("\n");
}
return 0;
}