http://acm.hust.edu.cn/vjudge/problem/32336/origin
题目大意:
海上有很多岛p 现在要在x轴上放置雷达,给出雷达范围和岛的位置,问至少
多少个雷达才可以侦查到全部岛。
贪心的想法是把雷达侦查范围尽量的铺开,侦查的范围不要重叠,这样的话就最大化的利用了雷达的侦查范围,没有浪费~这很贪心。
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct point
{
int x,y;
double xl,xr;
};
bool comp(point a,point b)
{
return a.x<b.x;
}
int n,d;
point pt[1005];
int solve(){
for(int i=0;i<n;i++)
if(pt[i].y>d)return -1; //雷达无法侦查到
sort(pt,pt+n,comp);
double r=pt[0].xr;
int ans=1;
for(int i=1;i<n;i++){
if(pt[i].xl>r){ //当区间不重叠
r=pt[i].xr;
ans++;
}
else if(pt[i].xl<r&&pt[i].xr<r) //区间重叠可以忽略
r=pt[i].xr;
}
return ans;
}
int main()
{
int cas=1;
while(scanf("%d%d",&n,&d)==2&&n&&d){
for(int i=0;i<n;i++){
scanf("%d %d",&pt[i].x,&pt[i].y);
double c=sqrt((double)d*(double)d-(double)pt[i].y*(double)pt[i].y);
pt[i].xl=(double)pt[i].x-c; //将岛变为侦查区间
pt[i].xr=(double)pt[i].x+c;
}
printf("Case %d: %d\n",cas++,solve());
}
}