「一本通 1.1 例 3」喷水装置(贪心算法)题解

「一本通 1.1 例 3」喷水装置(贪心算法)题解

题目描述

L L L 米,宽 W W W 米的草坪里装有 n n n 个浇灌喷头。每个喷头都装在草坪中心线上(离两边各 W 2 \dfrac{W}{2} 2W 米)。我们知道每个喷头的位置(离草坪中心线左端的距离),以及它能覆盖到的浇灌范围。

请问:如果要同时浇灌整块草坪,最少需要打开多少个喷头?

Code

#include <bits/stdc++.h>
using namespace std;
int n, t, l, w, yss, a, s;
struct gyro
{
	double x, y;
} f[15001];
bool cmp(gyro a, gyro b)
{
	return a.x < b.x;
}
int main()
{
	cin >> t;
	while (t--)
	{
		cin >> n >> l >> w;
		yss = 0;
		for (int i = 1; i <= n; i++)
		{
			cin >> a >> s;
			if (s <= w / 2)
				continue;
			yss++;
			f[yss].x = a - sqrt(s * s - w * w / 4.0);
			f[yss].y = a + sqrt(s * s - w * w / 4.0);
		}
		sort(f + 1, f + yss + 1, cmp);
		double d = 0;
		int ans = 0, bj = 1, i = 1;
		while (d < l)
		{
			ans++;
			double h = d;
			for (; f[i].x <= h && i <= yss; i++)
				if (d < f[i].y)
					d = f[i].y;
			if (d == h && h < l)
			{
				cout << -1 << endl;
				bj = 0;
				break;
			}
		}
		if (bj)
			cout << ans << endl;
	}
	return 0;
}

广告

绿树公司 - 官方网站:https://wangping-lvshu.github.io/LvshuNew/

绿树智能 - 官方网站:https://wangping-lvshu.github.io/LvshuZhineng/

(现在使用,人人均可获得300元大奖)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lvshu · 绿树

非常感谢您的搭讪

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值