题意:
给定很多个岛屿的坐标,然后在X轴上安装雷达,问把覆盖到所有岛屿最少需要几个雷达。不能覆盖到所有岛屿则输出“-1”。
根据题意,可以根据勾股定理将每个岛屿的坐标转化成在X轴安装雷达的区间。如果有一个岛屿的纵坐标 > d 则一定输出“-1”,否则就有解。
直接把转化完的区间按区间结束位置从小到大排序。随后贪心即可解决。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>
typedef long long LL;
const int INF = 500000001;
const double EPS = 1e-9;
const double PI = acos(-1.0);
using namespace std;
struct ak
{
double x, y;
}s[25001];
int cmp(const void *a, const void *b)
{
struct ak c = *(struct ak *)a;
struct ak d = *(struct ak *)b;
return c.y > d.y ? 1 : -1;
}
int main()
{
#ifdef _Test
freopen("test0.in", "r", stdin);
freopen("test0.out", "w", stdout);
srand(time(NULL));
#endif
int n, d, ans, flag, cnt = 1;
double r;
while(~scanf("%d %d", &n, &d), n != 0 || d != 0)
{
flag = 1;
int x, y;
for(int i = 0; i < n; i++)
{
scanf("%d %d", &x, &y);
if(y > d)
{
flag = 0;
}
if(!flag) continue;
double dif = sqrt(d*d - y*y);
s[i].x = x - dif;
s[i].y = x + dif;
}
printf("Case %d: ", cnt++);
if(flag)
{
qsort(s, n, sizeof(s[0]), cmp);
r = s[0].y;
ans = 1;
for(int i = 1; i < n; i++)
{
if(s[i].x > r)
{
r = s[i].y;
++ans;
}
}
printf("%d\n", ans);
}
else
{
printf("-1\n");
}
}
return 0;
}