uva 1615 高速公路(贪心,区间问题)

uva 1615 高速公路(贪心,区间问题)

给定平面上n个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里得距离不超过D。(n<=1e5)

对于每个点,可以找出在x轴上的对应线段,于是这道题就被转换成了一个区间选点问题。将所有线段都按右端点排好序,然后每次将点取在线段的最右端,如果覆盖不到最新线段就再创造一个新点,这样一直贪心的取下去即可。

#include <cmath>
#include <cctype>
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn=1e5+5;
double sqr(double x){ return x*x; }

int n, L, D;
struct Line{
    void set(double x, double y){
        l=x; r=y;
        if (l<0) l=0;
        if (r>L) r=L;
    }
    double l, r;
}line[maxn];

bool cmp1(Line &x, Line &y){
    return x.r<y.r; }

int main(){
    while (~scanf("%d", &L)){
        scanf("%d%d", &D, &n);
        double x, y, tmp;
        for (int i=0; i<n; ++i){
            scanf("%lf%lf", &x, &y);
            tmp=sqrt(sqr(D)-sqr(y));
            line[i].set(x-tmp, x+tmp);
        }
        sort(line, line+n, cmp1);
        double rgtest=-1; int tot=0;
        for (int i=0; i<n; ++i)
            if (line[i].l>rgtest){
            rgtest=line[i].r; ++tot; }
        printf("%d\n", tot);
    }
    return 0;
}
posted @ 2018-03-03 11:38  pechpo  阅读(243)  评论(0编辑  收藏  举报