题目的意思就是 在坐标轴中给出n个岛屿的坐标,以及雷达的扫描距离,要求在y=0线上放尽量少的雷达能够覆盖全部岛屿。
区间选点问题,
根据每个到所能被探测的范围排序即可.
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int N =1010;
int n, r, cas = 1;
struct Point {
double l, r;
} p[N];
int cmp(Point a, Point b) {
if (a.r != b.r)
return a.r < b.r;
return a.l > b.l;
}
int main() {
int x, y;
bool flag;
while (scanf("%d%d", &n, &r) && (n || r)) {
flag = true;
memset(p, 0, sizeof(p));
for (int i = 0; i < n; i++) {
scanf("%d%d", &x, &y);
if (flag) {
if (y > r) {
flag = false;
continue;
}
double t = sqrt((double)r * r - y * y);
p[i].l = x - t;
p[i].r = x + t;
}
}
printf("Case %d: ", cas++);
if (!flag)
printf("-1\n");
else {
sort(p, p + n, cmp);
int ans = 1;
double start = p[0].r;
for (int i = 1; i < n; i++) {
if (p[i].l - start <= 1e-4)
continue;
ans++;
start = p[i].r;
}
printf("%d\n", ans);
}
}
return 0;
}