zoukankan      html  css  js  c++  java
  • 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
    ", tot);
        }
        return 0;
    }
    
  • 相关阅读:
    Network(树形dp)洛谷2899
    2590 树的统计
    LCT 最小生成树
    几种贪心小结
    snmp
    div页面跳转
    2017.11.2总结,回顾及成果
    2017.11.1知识总结及回顾
    check,form,单选框与复选框总结
    HTML空格字符
  • 原文地址:https://www.cnblogs.com/MyNameIsPc/p/8496161.html
Copyright © 2011-2022 走看看