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

本文介绍了解决UVA1615高速公路问题的一种算法实现,该问题要求在平面上选择最少数量的x轴点,使得每个给定点到其中一个选定点的欧氏距离不超过给定值D。通过将问题转化为区间选点问题,并采用贪心策略进行求解。

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

转载于:https://www.cnblogs.com/MyNameIsPc/p/8496161.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值