ZOJ 1041 Transmitters

版权声明:虽然本蒟蒻很菜,但各位dalao转载请注明出处谢谢。 https://blog.csdn.net/xuxiayang/article/details/85344648

大意

n n 个点和一个坐标在 x , y x,y ,半径为 r r 的扫描器,问其在某个时刻最多能扫描到几个点


思路

刚学计算几何,做了zoj1041,发现洛谷竟然也有这道题,于是顺便A了写下这篇题解

这道题其实可以当做一道叉积的模板题

首先,当点距离中心长度大于 r r 显然是可以忽略的,所以我们只需要考虑剩下的点

我们发现,雷达扫描的范围一定是一个半圆,也就是说如果有 n n 个点,这次扫描到了 x x 个点,那么你一定也可以扫描到 n x n-x 个点,因为你同样可以扫描另一半

那么我们具体怎么模拟扫描呢?直接暴力扫描似乎有些不妥,易证,每次扫描的最边边的地方有点是最优的,也就是说,边界的落点只会落在实际存在的点上。

理解了这个之后问题就变得很简单了,我们暴力枚举分界点,判断其左边(或右边)有多少个点,同时判断一下另一半即可。

判断左右边有多少个点的过程可以用叉积

时间复杂度: O ( n 2 ) O(n^2)


代码

#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;double x,y,r,a,b,k;
int tot,n,special,normal,ans;
struct node{double x,y;}e[151];
inline double p(double x){return x*x;}
inline double cj(int a,int b,int c){return (e[b].x-e[a].x)*(e[c].y-e[a].y)-(e[b].y-e[a].y)*(e[c].x-e[a].x);}
signed main()
{
	while(scanf("%lf%lf%lf",&x,&y,&r)!=EOF)
	{
		if(r<0.0) break;//结束条件
		scanf("%d",&n);
		e[tot=0]=(node){x,y};
		for(register int i=1;i<=n;i++)
		{
			scanf("%lf%lf",&a,&b);
			if(sqrt(p(a-x)+p(b-y))<=r) e[++tot]=(node){a,b};//剔除超出范围的点
		}
		if(!tot) {puts("0");continue;}//一个都扫不到
		n=tot;ans=0;
		for(register int i=1;i<=n;i++)
		{
			normal=special=0;
			for(register int j=1;j<=n;j++)
			if(i!=j)
			{
				if((k=cj(0,i,j))<0) normal++;//在扫描范围
				else if(!k) special++;//在边界
			}
			ans=max(ans,max(special+normal+1,n-(special+normal)));//取最大值
		}
		printf("%d\n",ans);//输出
	}
}

猜你喜欢

转载自blog.csdn.net/xuxiayang/article/details/85344648
ZOJ