zoukankan      html  css  js  c++  java
  • AcWing:112. 雷达设备(贪心 + 笛卡尔坐标系化区间)

    假设海岸是一条无限长的直线,陆地位于海岸的一侧,海洋位于另外一侧。

    每个小岛都位于海洋一侧的某个点上。

    雷达装置均位于海岸线上,且雷达的监测范围为d,当小岛与某雷达的距离不超过d时,该小岛可以被雷达覆盖。

    我们使用笛卡尔坐标系,定义海岸线为x轴,海的一侧在x轴上方,陆地一侧在x轴下方。

    现在给出每个小岛的具体坐标以及雷达的检测范围,请你求出能够使所有小岛都被雷达覆盖所需的最小雷达数目。

    输入格式

    第一行输入两个整数n和d,分别代表小岛数目和雷达检测范围。

    接下来n行,每行输入两个整数,分别代表小岛的x,y轴坐标。

    同一行数据之间用空格隔开。

    输出格式

    输出一个整数,代表所需的最小雷达数目,若没有解决方案则所需数目输出“-1”。

    数据范围

    1n10001≤n≤1000

    输入样例:

    3 2
    1 2
    -3 1
    2 1
    

    输出样例:

    2

    算法:贪心 + 笛卡尔坐标系化区间

    题解:参考https://www.acwing.com/solution/acwing/content/1061/

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    
    using namespace std;
    
    #define INF 0x3f3f3f3f
    #define eps 1e-6
    const int maxn = 1e3+7;
    
    struct node {
        double l, r;
        friend bool operator < (node a, node b) {
            return a.r < b.r;
        };
    }arr[maxn];
    
    int main() {
        int n, d;
        scanf("%d %d", &n, &d);
        for(int i = 1; i <= n; i++){
            int x, y;
            scanf("%d %d", &x, &y);
            if(y > d) {     //当点的位置超过了半径长度是,输出-1
                printf("-1
    ");
                return 0;
            }
            double len = sqrt(d * d - y * y);       //求出覆盖长度的1/2
            arr[i].l = x - len;     //求出区间的两个端点
            arr[i].r = x + len;
        }
        sort(arr + 1, arr + n + 1);
        int ans = 0;
        double last = -INF;
        for(int i = 1; i <= n; i++) {
            if(arr[i].l > last + eps) {       //如果当前区间的左端点大于上一个区间的右端点的话,那么就新增一个雷达
                ans++;
                last = arr[i].r;
            }
        }
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    SpringMVC 多文件上传
    get传参乱码问题
    springMVC配置
    带参sql$和#的区别(注解)
    java多线程--实现Runnable接口方式
    java复制文件夹及所有子目录和文件
    Angularjs 学习笔记
    springboot 项目 docker化部署
    docker 基础
    Java-马士兵动态代理模式
  • 原文地址:https://www.cnblogs.com/buhuiflydepig/p/11298564.html
Copyright © 2011-2022 走看看