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

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

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

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

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

输入格式

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

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

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

输出格式

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

数据范围

1n10001≤n≤1000,
1000x,y1000−1000≤x,y≤1000

输入样例:

3 2
1 2
-3 1
2 1

输出样例:

2

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>

using namespace std;

int n,d,cnt=1;
vector<pair<double,double> >p;

bool cmp(struct pair<double,double> x,struct pair<double,double> y)//这种类型不会写,可以写auto
{
    return x.first<y.first;
}


int main()
{
    scanf("%d%d",&n,&d);
    
    
    for(int i=1;i<=n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        if(abs(y)>d)//注意是绝对值 
        {
            puts("-1");
            return 0;
        }
        p.push_back(make_pair(x-sqrt(d*d-y*y),x+sqrt(d*d-y*y)));
    }
    sort(p.begin(),p.end(),cmp);//sort容器只能那样写
    //区间合并不等于区间求交集
    double l=p[0].first,r=p[0].second;
    for(int i=1;i<n;i++)
    {
        if(r<p[i].first)
        {
            cnt++;
            l=p[i].first;
            r=p[i].second;
        }
        else {
             l=max(l,p[i].first);
             r=min(r,p[i].second);
        }
    }
    cout<<cnt;
    return 0;
}

  注意:记得转换主体,变成区间求交集的问题;

posted on 2022-08-10 23:05  浅唱\,,笑竹神易  阅读(40)  评论(0)    收藏  举报