「一本通 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元大奖)