链接
题目描述
有 n n n个建筑物,第 i i i个建筑物在笛卡尔坐标系上的坐标为 ( x i , y i ) (x_i, y_i) (xi,yi) ,你需要在 x x x轴上安装一些雷达,每个雷达的侦察半径均为 d d d ,要求每个建筑物都至少被一个雷达侦测到,求最少要安装几个雷达。
样例输入
3 2
1 2
-3 1
2 1
样例输出
2
思路
每个点所在的圆的圆心 x x x坐标的范围为
x − d 2 − y 2 ∼ x + d 2 − y 2 x - \sqrt{d^2 - y^2} \sim x + \sqrt{d^2 - y^2} x−d2−y2∼x+d2−y2
那么题目就转化为了一个数轴上有若干个区间,现在要添加最少的点,使得每个区间都含有一个点
那我们把每个区间的右端点排序,然后对于每个区间看看是否有交集,然后决定是否要添加新点即可
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int n, d, ans;
double x[1005], y[1005];
struct ww
{
double l, r;
}a[10005];
bool cmp(ww a, ww b)
{
return a.r < b.r;
}
int main()
{
scanf("%d%d", &n, &d);
for(int i = 1; i <= n; ++i)
{
scanf("%lf%lf", &x[i], &y[i]);
double h = abs(y[i]);
if(d < h) {
printf("-1");
return 0;
}
}
for(int i = 1; i <= n; ++i)
a[i].l = x[i] - sqrt(1ll * d * d - 1ll * y[i] * y[i]),
a[i].r = x[i] + sqrt(1ll * d * d - 1ll * y[i] * y[i]);
sort(a + 1, a + n + 1, cmp);
double now = a[1].r;
int t = 1;
for(int i = 2; i <= n; ++i)
{
if(a[i].l <= now && now <= a[i].r) continue;
else ans++, now = a[i].r;
}
ans++;
printf("%d", ans);
return 0;
}