题意:一个草坪长l宽w上有n个喷头,每个的位置(从草坪最左端开始计算)和每个喷头喷开后的覆盖的圆形面积,求出最少几个喷头能让草坪全部被覆盖到。
题解:最小覆盖问题,只要注意将圆的左范围和右范围与w的关系找到,不只是单纯的从pos处 -r 和 +r。
#include <stdio.h>
#include <string.h>
#include <math.h>
const int N = 10005;
struct S {
double st, en;
}s[N];
int main() {
int n, l, w;
while (scanf("%d%d%d", &n, &l, &w) != EOF) {
int num = 0;
for (int i = 0; i < n; i++) {
double pos, r;
scanf("%lf%lf", &pos, &r);
if (r <= w / 2.0)
continue;
double c = sqrt(r * r - w * w / 4.0);
s[num].st = pos - c;
s[num++].en = pos + c;
}
int ans = 0;
double right = 0;
while (right < l) {
double temp = right;
for (int i = 0; i < num; i++) {
if (s[i].st <= temp && s[i].en > right)
right = s[i].en;
}
if (right == temp) {
ans = -1;
break;
}
ans++;
}
printf("%d\n", ans);
}
return 0;
}