【ybtoj 高效进阶 1.2】 【贪心】 雷达装置

【ybtoj 高效进阶 1.2】 【贪心】 雷达装置

题目

在这里插入图片描述
在这里插入图片描述


解题思路

利用勾股定理求出每个点以d为半径所可以覆盖的区间
勾股:a2+b2=c2,l=x-sqrt(d2-y2),r=x+sqrt(d2-y2)
如果这个点的纵坐标大于d,直接输出-1
如果两个区间有交集,那么共用一个雷达即可
最后找有多少个区间两两不相交


代码

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
struct lzf{
    
    
	double l,r;
}a[1200];
long long n,d,ans;
double r,x,y;
bool cmp(lzf l,lzf y)
{
    
    
	 return l.r<y.r;
}
int main()
{
    
    
	scanf("%lld%lld",&n,&d);
	for (int i=1;i<=n;i++)
	{
    
    		scanf("%lf%lf",&x,&y);
		if (abs(y)>d)  
		{
    
    
		   printf("-1\n");
		   return 0;
		}
		double c=sqrt(d*d-y*y);
		a[i].l=x-c;
		a[i].r=x+c;
	}
	sort(a+1,a+n+1,cmp);
	r=a[1].r;
	ans=1;
	for (int i=2;i<=n;i++)
	    if (r<a[i].l)
	    {
    
    
	    	ans++;
	    	r=a[i].r;
		}
	printf("%d",ans);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45621109/article/details/111727215