【YBTOJ】【贪心】雷达装置

这篇博客探讨了一种利用贪心算法和勾股定理来确定坐标点在给定半径内的覆盖问题。通过读取多个点的坐标,计算它们对应的圆的范围,并进行排序,最终找到最小的覆盖数。代码实现中,通过比较相邻圆的范围来优化解决方案,当一个点的坐标超出半径时,输出-1作为无效输入。
摘要由CSDN通过智能技术生成

在这里插入图片描述


思路:

贪心
坐标范围可以用勾股来确定
-1的判断就看纵坐标是否大于半径即可


C o d e Code Code:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <algorithm> 
#define rr register
using namespace std;

int n, d, ans = 1,x[1001],y[1001];
struct node {double l,r;}e[10005];
bool cmp (node xx,node yy){xx.r < yy.r;}//排序
int main ()
{
	scanf ("%d%d", &n, &d);
	for (rr int i = 1; i <= n; ++i)
	{
		scanf("%d%d",&x[i],&y[i]);
		if (abs(y[i]) > d){printf("-1");return 0;}
		e[i].l = (double)x[i] - (double) sqrt(d * d - y[i] * y[i]);//勾股确定范围
		e[i].r = (double)x[i] + (double) sqrt(d * d - y[i] * y[i]);
	}
	sort(e + 1 , e + 1 + n , cmp);
	double now = e[1].r;
	for(rr int i = 2; i <= n; ++i)
	{
		if(e[i].l <= now && now <= e[i].r) continue;//贪心找最优解
		else {ans++; now = e[i].r;}
	}
	printf("%d", ans);
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值