雷达设备 贪心 区间处理 思维跳跃

雷达设备
在这里插入图片描述
输入样例

3 2
1 2
-3 1
2 1

输出样例

2

⭐ 思路

将处理雷达能覆盖的岛屿圆,转变成岛屿能接收到信号的雷达区间

🥚 一个小岛一个区间,要求每个区间都要有雷达
🥚 勾股定理算出所有的区间 sqrt(d^2 - y^2)= r : [x-r, x+r] 就是该岛屿能接收信号的区间

🥚 所有右端点排序,每次选右端点装置雷达 ,将能覆盖的区间全部去掉
import java.util.*;

public class Main
{
	static int N = 1010;
	static double INF = 1e10;
	static double eps = 1e-6;// 处理精度差

	static class Pair
	{
		double l;
		double r;

		public Pair(double l, double r)
		{
			super();
			this.l = l;
			this.r = r;
		}
	}

	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int d = sc.nextInt();
		Pair[] a = new Pair[n];
		for (int i = 0; i < n; i++)
		{
			int x = sc.nextInt();
			int y = sc.nextInt();
			if (y > d)
			{
				System.out.println(-1);
				System.exit(0);
			}
			double t = Math.sqrt((d * d - y * y));
			a[i] = new Pair(x - t, x + t);
		}

		Arrays.sort(a, (o1, o2) -> Double.compare(o1.r, o2.r));
		int res = 0;
		double last = -INF;
		for (int i = 0; i < n; i++)
		{
			if (a[i].l > last + eps)
			{
				res++;
				last = a[i].r;
			}
		}
		System.out.println(res);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值