雷达设备
题目
假设海岸是一条无限长的直线,陆地位于海岸的一侧,海洋位于另外一侧。
每个小岛都位于海洋一侧的某个点上。
雷达装置均位于海岸线上,且雷达的监测范围为d,当小岛与某雷达的距离不超过d时,该小岛可以被雷达覆盖。
我们使用笛卡尔坐标系,定义海岸线为x轴,海的一侧在x轴上方,陆地一侧在x轴下方。
现在给出每个小岛的具体坐标以及雷达的检测范围,请你求出能够使所有小岛都被雷达覆盖所需的最小雷达数目。
输入格式
第一行输入两个整数n和d,分别代表小岛数目和雷达检测范围。
接下来n行,每行输入两个整数,分别代表小岛的x,y轴坐标。
同一行数据之间用空格隔开。
输出格式
输出一个整数,代表所需的最小雷达数目,若没有解决方案则所需数目输出“-1”。
数据范围
1 ≤ n ≤ 1000 1≤n≤1000 1≤n≤1000
输入样例
3 2
1 2
-3 1
2 1
输出样例
2
题解
思路
- 本题可以转化为区间贪心问题
- 具体解法见:AcWing 112. 雷达设备
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 1010;
int n, d, cnt;
pair<double, double> a[N];
bool cmp(pair<double, double> &a, pair<double, double> &b) {
return a.second < b.second;
}
int main () {
cin >> n >> d;
int x, y;
bool fail = false;
for (int i = 0; i < n; i ++) {
cin >> x >> y;
if (y > d) {
fail = true;
}
else {
double t = sqrt(d * d - y * y);
a[i] = {double(x - t), double(x + t)};
}
}
if (fail == true) {
puts("-1");
}
else {
sort(a, a + n, cmp);
double last = -1e5;
for (int i = 0; i < n; i ++) {
if (last < a[i].first) {
last = a[i].second;
cnt ++;
}
}
cout << cnt;
}
return 0;
}