poj 1328 Radar Installation

        题意:平面上有一些点,你需要在x轴上添加一些半径为d的圆,使得所有点都在圆的范围内,求最少添加圆的数量。

        思路:贪心。对每个点,计算圆能把它包含在内的最大横坐标,对它排序、然后按顺序检查一遍就好了。


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
using namespace std;
const double eps=1e-8;
inline double fabs(double x){
    if(x<0)x=-x;
    return x;
}
struct Node{
    int x,y;
    double a;
    bool operator<(const Node& t)const{
        return a<t.a;
    }
};
Node node[1010];
int n;
int d;
inline bool judge(double x,double y,double radar){
    if( (double)d*d >=fabs(x-radar)*fabs(x-radar)+y*y  ){
        return 1;
    }
    return 0;
}
int main(){
    int cas=0;
    while(cin>>n>>d){
        cas++;
        if(n==0&&d==0)break;
        bool ok=1;
        for(int i=0;i<n;i++){
            scanf("%d%d",&node[i].x,&node[i].y);
            if(node[i].y>d)ok=0;
            if(ok)node[i].a=node[i].x+sqrt(d*d-node[i].y*node[i].y);
        }
        if(!ok){
            printf("Case %d: ",cas);
            cout<<-1<<endl;
            continue;
        }
        sort(node,node+n);
        
        int ans=0;
        for(int i=0;i<n;){
                double radar=node[i].a;
                ans++;
                i++;
                while(judge(node[i].x,node[i].y,radar)&&i<n){
                    i++;
                }
        }
        printf("Case %d: ",cas);
        cout<<ans<<endl;
    }
    return 0;
}


你可能感兴趣的:(poj 1328 Radar Installation)