首页 > 其他 > 详细

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

时间:2018-03-03 13:19:01      阅读:196      评论:0      收藏:0      [点我收藏+]

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;
}

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

原文:https://www.cnblogs.com/MyNameIsPc/p/8496161.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!