逆向思维,假设岛屿是雷达,映射到(x) 轴的区间取交集,放置一个雷达即可
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
const int N = 1e3 + 10;
struct node {
double l,r;
}c[N];
bool cmp(node a,node b) {
if(a.l != b.l) return a.l < b.l;
return b.r < a.r;
}
int main() {
int cnt = 0,n,d;
while(cin >> n >> d && n && d) {
bool ok = 1;
for(int i = 0;i < n; ++i) {
int x,y;
cin >> x >> y;
if(y > d) ok = 0;
double dx = sqrt(d * d * 1.0 - y * y);
c[i].l = x - dx;
c[i].r = x + dx;
}
if(ok == 0) {
cout << "Case " << ++cnt << ": -1" << endl;
continue;
}
sort(c,c + n,cmp);
double r = c[0].r;
int ans = 1;
for(int i = 0;i < n; ++i) {
if(c[i].l <= r) r = min(r,c[i].r);
else r = c[i].r,ans ++;
}
cout << "Case " << ++cnt << ": " << ans << endl;
}
return 0;
}
参考