UVa 1615 - Highway(贪心)

给出n个点和一个值D,在x轴选出最少的点使得对于每个给出点都有一个选出的点离它的欧几里德距离不超过D。
将给出的点转化成区间,就变成了简单的区间选点问题。

#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
double L,D;
struct node{
    double l,r;
    node(const double &x,const double &y){
        double R=sqrt(D*D-y*y);
        this->l=max(x-R,0.0),this->r=min(x+R,L);
        return;
    }
    bool operator < (const node &x) const {
        return r==x.r?l>x.l:r<x.r;
    }
};
vector<node> vil;
int main(){
    while(~scanf("%lf%lf",&L,&D)){
        vil.clear();
        int n,cnt=1;
        scanf("%d",&n);
        for(int i=0;i<n;++i){
            double x,y;
            scanf("%lf%lf",&x,&y);
            vil.push_back(node(x,y));
        }
        sort(vil.begin(),vil.end());
        double pos=vil[0].r;
        for(int i=0;i<(int)vil.size();++i){
            if(pos<=vil[i].r&&pos>=vil[i].l) continue;
            pos=vil[i].r,++cnt;
        }
        printf("%d\n",cnt);
    }
    return 0;
}

你可能感兴趣的:(ACM,uva,贪心法)