题意:有n个小岛,每个小岛有xy坐标,x坐标轴上可以安雷达,给出雷达半径d,求将所有小岛覆盖的最小雷达数
思路:在每个小岛处以d画圆,与x轴相交处的点都能安装雷达,每个小岛都选出与x轴相交的区间,然后用这些区间转化为区间选点问题贪心求解
区间选点:将区间按照r从小到大排列,r相同时l递减排列,则将点取在r最小处覆盖的区间最多,如下图中黑点覆盖区间比灰点多
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
using namespace std;
int n, d, p[1005][2], ans;
struct Node {
double l;
double r;
}a[1005];
bool cmp(const Node& n1, const Node& n2)
{
if (n1.r != n2.r)
return n1.r < n2.r;
else
return n1.l > n2.l;
}
int main()
{
int T = 0;
while (cin >> n >> d && n && d) {
ans = 1;
int ok = 1;
for (int i = 0; i < n; i++) {
cin >> p[i][0] >> p[i][1];
if (p[i][1] > d)
ok = 0;
double k = sqrt(d * d - p[i][1] * p[i][1]);
a[i].l = p[i][0] - k;
a[i].r = p[i][0] + k;
}
int i = 0;
sort(a, a+n, cmp);
double t = a[0].r;
for (int i = 1; i < n; i++) {
if (a[i].l > t) {
t = a[i].r;
ans++;
}
}
if (!ok)
ans = -1;
printf("Case %d: %d\n", ++T, ans);
}
}