**- 将问题转化为区间覆盖问题
- 此题求的是需要多少个头尾不相交的区间
- 贪心策略:按区间的左端点排序,若i的左端点与i-1的右端点不相交(在精度范围内)就增加一个区间**
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
int n, r;
typedef pair<double, double> pdd;
const int N = 1010;
const double eps = 1e-6, INF = 1e10;
pdd segs[N];
int main()
{
cin>>n>>r;
bool right = true;
for (int i = 0; i < n; i++)
{
double x, y;
cin >> x >> y;
if (y > r)
{
right = false;
break;
}
double len = sqrt(r*r - y * y);
segs[i] = { x + len,x - len };//first存的是右端点,second存左端点
}
if (!right)puts("-1");
else
{
sort(segs, segs + n);
int res = 0;
double last = -INF;//第一个肯定是要建一个的,所以是“负无穷”
for (int i = 0;i < n; i++)
{
auto seg = segs[i];
if (seg.second > last + eps)
{
res++;
last = seg.first;
}
}
cout << res << endl;
}
return 0;
}
done!