题目
思路来源于csdn的其他博客,由小岛的位置推出雷达的区间,通过雷达的区间求出雷达的最少数量
#include <iostream>
#include <cstdio>
#include <cmath>
#include <iomanip>
#include <vector>
#include <map>
#include <cstring>
#include <algorithm>
using namespace std;
struct Interval{
double begin;
double end;
};
typedef long long ll;
int N, D;
Interval radar[1001];
bool Cmp(Interval a, Interval b)
{
if (a.begin != b.begin) {
return a.begin < b.begin;
}
return a.end < b.end;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int cnt = 0;
while (cin >> N >> D && !(N == 0 && D == 0) ) {
double x, y;
bool flag = true;
for (int i = 0; i < N; i++) {
cin >> x >> y;
if (y > D) {
flag = false;
}
else {
radar[i].begin = x - sqrt(D * D - y * y);
radar[i].end = x + sqrt(D * D - y * y);//勾股定理
}
}
if (!flag) {//特殊情况
cout << "Case " << ++cnt << ": " << -1 << endl;
continue;
}
sort(radar, radar + N, Cmp);
int ans = 1;
Interval last = radar[0];
for (int i = 1; i < N; i++) {//通过区间求出最小雷达数量
if (radar[i].begin <= last.end) {
last.begin = radar[i].begin;
if (radar[i].end < last.end) {
last.end = radar[i].end;
}
}
else {
ans++;
last = radar[i];
}
}
cout << "Case " << ++cnt << ": " << ans << endl;
}
return 0;
}