题目链接
考虑一下每个小岛被覆盖时雷达的区间, 最后就把各个小岛转化为了一个个区间,转化为了区间贪心问题,即用最少的点覆盖所有的区间 ,具体的贪心算法就是对于每个岛不断的和下一个岛判断是否有交集, 有的话再判断是否改变集合的边界。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;
const int MAX=0x3f3f3f3f;
const int N=1010;
int max_y=-1;
struct _land{
double s,e;
}land[N];
bool cmp(_land a,_land b){
return a.s<b.s;
}
int main(){
int n,d,kase=1;
while(cin>>n>>d&&(n||d)){
max_y=-1;
int ans=0;
for(int i=0;i<n;i++){
int x,y;
cin>>x>>y;
max_y=max(max_y,y);
double len=sqrt(double(d*d-y*y));
land[i].s=x-len;
land[i].e=x+len;
}
if(max_y>d) ans=-1;
else{
sort(land,land+n,cmp); //区间贪心
double range=-MAX;
for(int i=0;i<n;i++){
if(land[i].s<=range) range=min(range,land[i].e);
else{
range=land[i].e;
ans++;
}
}
}
printf("Case %d: %d\n",kase++,ans);
}
}