雷达设备


#include<cstring>
#include<string>
#include<iostream>
#include<vector>
#include<climits>
#include<math.h>
#include<algorithm>
#define ll long long
#define  pdd pair<double,double>
using namespace std;
const int N=1e6+10;
vector<pdd>a;
bool cmp (const pdd x,const pdd y)
{
    return x.second < y.second;
}
int main(){
    int n ;
    double m;
    cin >> n >> m;
    bool su = false ;
    for(int i = 0; i < n;i ++){
        double x, y;
        cin >> x >> y;
        if(y > m)su = true;
        a.push_back({x - sqrt(m * m - y * y), x + sqrt(m * m - y * y)});
    }
    if(su){
        puts("-1");
        return 0;
    }
    sort (a.begin(),a.end(),cmp);
    double r = a[0].second;
    int cnt = 1;
    for(int i = 1;i < a.size();i ++)
    {
        if(a[i].first <= r)
        {
            continue;
        }
        else {
            cnt ++;
            r = a[i].second;
        }
    }
    cout << cnt << endl;
    return 0;
}

本题考查的是贪心算法,每个小岛都有一个范围,在这个范围内,安装雷达可以检测到小岛。因此我们只需要将每个小岛的范围求出来,通过对右端点进行排序(当然对左端点进行排序也是可以的),如果第i个小岛的右端点在第i+1个小岛的左断点外,那么这两个小岛就可以共享一个雷达。

全部评论

相关推荐

AI牛可乐:哇,听起来你很激动呢!杭州灵枢维度科技听起来很厉害呀~你逃课去白马培训,老冯会同意吗?不过既然你这么感兴趣,肯定是有原因的吧! 对了,想了解更多关于这家公司或者求职相关的问题吗?可以点击我的头像私信我哦,我可以帮你更详细地分析一下!
你都用vibe codi...
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务