本文搬运自本人初中博客园博客,若图片加载不出来,可到原文查看:https://www.cnblogs.com/zhangtingxi/p/15643380.html

题目链接

题目

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

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

思路

首先直径不足 ww 直接排除。

然后我们希望每个在覆盖到左边的情况下越右越好。

所以我们按左端点排序后贪心取右端点即可。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Problem: #10002. 「一本通 1.1 例 3」喷水装置
// Contest: LibreOJ
// URL: https://loj.ac/p/10002
// Memory Limit: 512 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
//#define N
struct node
{
double l, r;
}a[15010];
int n, m, i, j, t;
double w, l, x, k;
double nwmxrght, mnlft;
int ans;

bool cmp(node x, node y)
{
return x.l<y.l;
}

void work()
{
// if(a[n].r<l)
// {
// printf("-1\n");
// return ;
// }
i=1;
mnlft=0; nwmxrght=0; ans=0;
while(nwmxrght<l)
{
nwmxrght=mnlft;
if(a[i].l>mnlft)
{
printf("-1\n");
return ;
}
for(; a[i].l<mnlft&&i<=n; ++i)
nwmxrght=max(nwmxrght, a[i].r);
mnlft=nwmxrght;
++ans;
}
printf("%d\n", ans);
return ;
}

signed main()
{
// freopen("tiaoshi.in", "r", stdin);
// freopen("tiaoshi.out", "w", stdout);
t=read();
while(t--)
{
n=read();
scanf("%lf%lf", &l, &w);
m=0;
for(i=1; i<=n; ++i)
{
scanf("%lf%lf", &x, &k);
if(k<=w/2) continue;
a[++m].l=x-sqrt(k*k-w*w/4);
a[m].r=x+sqrt(k*k-w*w/4);
}
n=m;
sort(a+1, a+n+1, cmp);
work();
}
return 0;
}