题目大意:海上有n个小岛(的坐标), 海岸线(x轴)上安装雷达, 使雷达能够覆盖所有的小岛,然后给出雷达覆盖半径,求出最少安装几个雷达能覆盖所有的小岛。
解题思路:一看很明确就是用区间覆盖的贪心, 但是由于是圆,不好算,但是所有雷达的纵坐标是确定的, 因此我们把问题能转换成x左边的情况,也就是区间覆盖了。 要想覆盖这个岛, 设小岛坐标为(x, y),则能覆盖它的圆心必须位于[x-sqrt(d*d - y*y), x+sqrt(d*d - y*y) ] 内
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int n, d;
struct Point
{
double l, r;
bool operator<(const Point & a) const
{
return this->l < a.l;
}
}P[1005];
int main()
{
int Case = 0;
while(scanf("%d%d", &n, &d) && n+d)
{
int ok = 1;
int x, y;
for(int i=0; i<n; ++i)
{
scanf("%d%d", &x, &y);
if(y > d) ok = 0;
if(!ok) continue;
P[i].l = (double)x - sqrt((double)d*d - y*y);
P[i].r = (double)x + sqrt((double)d*d - y*y);
}
if(!ok)
{
printf("Case %d: -1\n", ++Case);
continue;
}
sort(P, P+n);
double now = P[0].r;
int cnt = 1;
for(int i=0; i<n; ++i)
{
if(P[i].r < now) now = P[i].r;
if(P[i].l > now)
{
++cnt;
now = P[i].r;
}
}
printf("Case %d: %d\n", ++Case, cnt);
}
return 0;
}