大意是通过最少的雷达控制所有的小岛,本质上是取多个点覆盖所有线段的问题。
开始想错了XD以为覆盖了远处的近处的也会被覆盖,后来发现自己傻了233333
以每个小岛为中心,dist为半径画圆和x轴相交le与ri两个点,这两点中间放雷达就能够覆盖这个小岛,相当于把问题等价成了线段的相互覆盖的贪心问题。
这种贪心方法是把小岛的ri从小到大排列,然后从小向大扫,初始点是第一个的ri,如果发现有一条线段不被覆盖,就加一个点,这个点的坐标就是这条线段的ri,可以证明是最优解。
#pragma warning (disable:4996)
/*
coded by fgtmiao
time:2019/6/30
*/
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int dist;
class DD
{
public:
float le, ri;
DD(float xx, float yy):le(xx),ri(yy){}
DD() {}
};
bool operator<(DD a, DD b)//打扰了,排序按照各个线段的左边
{
return a.ri - b.ri < 1e-5;
}
int main()
{
int n;
int cnn = 0;
while (1)
{
cin >> n>>dist;
cnn++;
if (n == 0&&dist==0)
{
return 0;
}
if (n == 0)
{
printf("Case %d: 0\n", cnn);
continue;
}
DD all_set[1003];
float tmpx, tmpy;
bool end = false;
for (int i = 0; i < n; i++)
{
cin >> tmpx >> tmpy;
all_set[i].le = tmpx - sqrt(dist*dist - tmpy * tmpy);
all_set[i].ri = tmpx + sqrt(dist*dist - tmpy * tmpy);//找到每个小岛的对应的线段
if (tmpy > dist)
{
end = 1;
}
}
if (end)
{
printf("Case %d: -1\n", cnn);
continue;
}
sort(all_set, all_set + n);//一串
int cnt = 1;
float now = all_set[0].ri;//第一个点
for (int i = 1; i < n; i++)
{
if (all_set[i].le > now)
{
now = all_set[i].ri;
cnt++;
}
}
printf("Case %d: %d\n", cnn, cnt);
}
}