POJ2398 判断点在线左边

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liufengwei1/article/details/86557009

跟POJ2318差不多,不过这次要对线进行排序,对线的排序有涉及到对点的排序

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define maxl 100010
#define eps 1e-8

using namespace std;

int n,m;;
double L,U,R,D;
int ans[maxl],num[maxl];

struct point
{
	double x,y;
	point (double a=0,double b=0)
	{
		x=a;y=b;
	}
	point operator - (const point &b) const
	{
		return point(x-b.x,y-b.y);
	}
	double operator * (const point &b) const
	{
		return x*b.x+y*b.y;
	}
	double operator ^ (const point &b) const
	{
		return x*b.y-y*b.x;
	}
	bool operator < (const point &b) const
	{
		return x<b.x-eps;
	}
	bool operator == (const point &b) const 
	{
		return x>b.x-eps && x<b.x+eps && y>b.y-eps && y<b.y+eps;
	}
};

struct line
{
	point s,e;
	double k;
	line(point a=point(),point b=point())
	{
		s=a;e=b;
		k=atan2(e.y-s.y,e.x-s.x);
	}
	bool operator < (const line &b) const 
	{
		return s < b.s;
	}
	bool operator == (const line &b) const
	{
		return s==b.s;
	}
}a[maxl];

inline void prework()
{
	scanf("%d%lf%lf%lf%lf",&m,&L,&U,&R,&D);
	double x1,x2;
	point p1,p2;
	for(int i=1;i<=n;i++)
	{
		scanf("%lf%lf",&x1,&x2);
		p1=point(x1,U);
		p2=point(x2,D);
		a[i]=line(p1,p2);
	}
	sort(a+1,a+1+n);
	for(int i=0;i<=n;i++)
		ans[i]=0;
	for(int i=0;i<=m;i++)
		num[i]=0;
}

inline bool isleft(point p,int id)
{
	point p1=a[id].s-p;
	point p2=a[id].e-p;
	if((p1^p2) < 0+eps)
		return true;
	else
		return false;
}

inline void mainwork()
{
	point p;int l,r,mid;
	for(int i=1;i<=m;i++)
	{
		scanf("%lf%lf",&p.x,&p.y);
		l=1,r=n;
		while(l+1<r)
		{
			mid=(l+r)>>1;
			if(isleft(p,mid))
				r=mid;
			else
				l=mid;
		}
		if(isleft(p,l))
			ans[l-1]++;
		else if(isleft(p,r))
			ans[r-1]++;
		else
			ans[r]++;
	}
	for(int i=0;i<=n;i++)
		num[ans[i]]++;
}

inline void print()
{
	puts("Box");
	for(int i=1;i<=m;i++)
	if(num[i]>0)
		printf("%d: %d\n",i,num[i]);
}

int main()
{
	while(~scanf("%d",&n) && n)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/liufengwei1/article/details/86557009
今日推荐