Radar Installation
Radar Installation
题解:区间选点问题
为什么是区间选点呢?关键是我们反着来看,如果将岛屿看成圆心,我们以雷达照射范围d为半径,我们可以X轴上找到一段区间,这段区间都可以放雷达使得该岛屿能被照射到,所以一个个雷达就产生了一个个区间,让雷达数最少覆盖所有区间,不就是区间选点嘛
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define endl '\n'
using namespace std;
const int mod = 1e9 + 7;
const double eps = 1e-9;
struct node
{
double l, r;
bool operator<(const node &t) const
{
return r < t.r;
}
} a[1010];
int main(void)
{
// Zeoy;
int t = 1;
// cin >> t;
while (t--)
{
int cnt = 1;
int n, d;
while (cin >> n >> d)
{
if (!n && !d)
break;
printf("Case %d: ", cnt++);
int flag = 1;
for (int i = 1; i <= n; ++i)
{
double x, y;
cin >> x >> y;
a[i].l = x - sqrt(d * d - y * y);
a[i].r = x + sqrt(d * d - y * y);
if (y > d)
{
flag = 0;
}
}
if (!flag)
{
cout << "-1\n";
continue;
}
sort(a + 1, a + n + 1);
double now = a[1].r;
int ans = 1;
for (int i = 2; i <= n; ++i)
{
if (now < a[i].l)
{
now = a[i].r;
ans++;
}
}
cout << ans << endl;
}
}
return 0;
}