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;
    }
    
  • 相关阅读:
    《数据结构》C++代码 线性表
    《数据结构》C++代码 Splay
    《数据结构》C++代码 前言
    蓝桥杯- 算法提高 最大乘积
    HDU-1241 Oil Deposits
    一个简单的网站计数器
    编写一个jsp页面,输出九九乘法表。
    Sum It Up
    历届试题 剪格子
    历届试题 分糖果
  • 原文地址:https://www.cnblogs.com/MyNameIsPc/p/8496161.html
Copyright © 2011-2022 走看看