题意:
一长l宽w的草坪,水平中心线上有n个位置pi可安装喷水装置,其覆盖范围为以半径ri为圆。求最少需要多少个喷水装置。
思路:
首先,每个装置在长方形草坪上的有效覆盖面积为 左边界pos - sqrt((r*r - w*w / 4.0 ))到右边界 pos + sqrt((r*r - w*w / 4.0 ))宽l的长方形。这样记录下有效区间后此题就可转为经典区间覆盖问题。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N = 10005;
int n;
double l, w;
struct spr {
double x, y;
}d[N];
int cmp(spr a, spr b){
return a.x < b.x ? 1 : 0;
}
int main(){
while (~scanf("%d%lf%lf", &n, &l,&w)) {
double pos, r;
int len = 0;
for (int i = 0; i < n; i++) {
scanf("%lf%lf", &pos, &r);
if (r > w / 2) {
d[len].x = pos - sqrt((r*r - w*w / 4.0 ));
d[len].y = pos + sqrt((r*r - w*w / 4.0 ));
len++;
}
}
sort(d, d + len, cmp);
int ans = 0, flag = 0;
double left = 0, right = 0;
if (d[0].x <= 0) {
int i = 0;
while (i < len) {
int j = i;
while (j < len&& d[j].x <= left) {
if (right < d[j].y)
right = d[j].y;
j++;
}
if (j == i)
break;
ans++;
i = j;
left = right;
if (left >= l) {
flag = 1;
break;
}
}
}
if (flag)
printf("%d\n", ans);
else
printf("-1\n");
}
return 0;
}