http://acm.hust.edu.cn/vjudge/contest/view.action?cid=43009#problem/A
题目意思:
一个矩形box,给出左上和右下坐标,以及分隔板和玩具的数量,对于每个玩具都会扔到0-n个部分去,求被分隔的每个部分的玩具数量。
算法分析:
判断点在直线的一侧。叉积可以做到。PA*PB : x1*y2-x2*y1>=<0。
右手螺旋,拇指朝下为负-,即PA在PB左边。
拇指朝上为正+,即PA在PB的右边。
巧妙记忆法:右手螺旋右边为正。
代码如下:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define MAX 5000+1
using namespace std;
struct node
{
int x,y;
}toy;
int n,m;
node LU,RL;
int UP[MAX],LW[MAX];
int ans[MAX];
bool xmult(node a,int ux,int lx)
{//x1*y2-x2*y1
int x1,y1,x2,y2;
x1=ux-a.x,y1=LU.y-a.y;
x2=lx-a.x,y2=RL.y-a.y;
if((x1*y2-x2*y1)<0)
return true;
return false;
}
int main()
{
int i,j;
bool FR=false;
while(scanf("%d",&n),n)
{
memset(ans,0,sizeof(ans));
scanf("%d",&m);
scanf("%d%d%d%d",&LU.x,&LU.y,&RL.x,&RL.y);
for(i=0;i<n;i++)
scanf("%d%d",&UP[i],&LW[i]);
for(i=0;i<m;i++)
{
scanf("%d%d",&toy.x,&toy.y);
for(j=0;j<n;j++)
{
if(xmult(toy,UP[j],LW[j]))
break;
}
ans[j]++;
}
if(FR)printf("\n");
else FR=true;
for(i=0;i<=n;i++)
printf("%d: %d\n",i,ans[i]);
}
return 0;
}