POJ2398 判断点在线左边

跟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;
}

2019.2.11模板更新:

#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];
 
inline int sgn(double x)
{
	if(fabs(x)<eps) return 0;
	if(x<0) 
		return -1;
	else
		return 1;
}
 
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);
	}
    //按照x坐标排序
    bool operator < (const point &b)const
	{
		return x<b.x-eps;
	}
	bool operator == (const point &b)const 
	{
		return sgn(x-b.x)==0 && sgn(y-b.y)==0;
	}
	friend point operator * (const point &a,const double &b)			//数乘
	{
		return point(b*a.x,b*a.y);
	}
	friend point operator * (const double a,const point &b)
	{
		return point(a*b.x,a*b.y);
	}
	void transxy(double sinb,double cosb)
	{						//逆时针旋转,给出sin,cos 
		double tx=x,ty=y;
		x=tx*cosb-ty*sinb;
		y=tx*sinb+ty*cosb;
	}
	void transxy(double b)//逆时针旋转b弧度
	{
		double tx=x,ty=y;
		x=tx*cos(b)-ty*sin(b);
		y=tx*sin(b)+ty*cos(b);
	}
	double norm()
	{
		return sqrt(x*x+y*y);
	}
};
inline double det(const point &a,const point &b)
{					//叉积 
	return a.x*b.y-a.y*b.x;
}
inline double dot(const point &a,const point &b)
{					//点积 
	return a.x*b.x+a.y*b.y;
}
inline double dist(const point &a,const point &b)
{
	return (a-b).norm();
}
 
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(det(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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值