poj 2318 TOYS

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

struct POINT 
{
	int x,y;
};
POINT card_line[10010],toy_point[5010];
POINT left,right;

bool cmp(POINT &p1,POINT &p2)
{
	return p1.x<p2.x;
}

int det(POINT p1,POINT p2,POINT p3)
{
	return (p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y);
}

int main()
{
	int m,n,j;
	int i,sum[10010];
	while(1)
	{
		memset(sum,0,sizeof(sum));
		scanf("%d",&n);
		if(n==0) break;
		scanf("%d%d%d%d%d",&m,&left.x,&left.y,&right.x,&right.y);
		for (i=0;i<2*n;i=i+2)
		{
			scanf("%d%d",&card_line[i].x,&card_line[i+1].x);
			card_line[i].y=left.y;
			card_line[i+1].y=right.y;
		}
		for (i=0;i<m;i++)
		{
			scanf("%d%d",&toy_point[i].x,&toy_point[i].y);
		}
		//按x从小到大排序
		sort(toy_point,toy_point+m,cmp);
		for (i=0;i<m;i++)
		{
			for (j=0;j<2*n;j+=2)
			{
				int t1=det(toy_point[i],card_line[j],card_line[j+1]);
				if(j==0 && t1<0)
				{
					sum[j]++;
					break;
				}
				else if(t1<0)
				{
					int t2=det(toy_point[i],card_line[j-2],card_line[j-1]);
				    if(t2>0)
					{
						sum[j]++;
						break;
					}
				}
			}
			if(j==2*n) sum[j]++;
		}
		int cas=0;
		for (i=0;i<=2*n;i+=2)
			printf("%d: %d\n",cas++,sum[i]);
		printf("\n");
	}
	return 0;
}

你可能感兴趣的:(poj 2318 TOYS)