题目链接:http://poj.org/problem?id=1328
题目:Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.
We use Cartesian coordinate system, defining the coasting is the x-axis. The sea side is above x-axis, and the land side below. Given the position of each island in the sea, and given the distance of the coverage of the radar installation, your task is to write a program to find the minimal number of radar installations to cover all the islands. Note that the position of an island is represented by its x-y coordinates.
![](https://i-blog.csdnimg.cn/blog_migrate/f6ffe515205096387436c13c7449b0ed.jpeg)
Figure A Sample Input of Radar Installations
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
struct s_region
{
double beg, end;
}data[1000];
double x[1000], y[1000];
bool visited[1000];
bool cmp (s_region a, s_region b)
{
return a.beg < b.beg;
}
int main()
{
int numIsland;
double dist;
int casei = 0;
while (scanf ("%d%lf", &numIsland, &dist) != EOF)
{
if (numIsland == 0 && dist == 0.0)
break;
for (int i = 0; i < numIsland; ++i)
{
scanf ("%lf%lf", &x[i], &y[i]);
}
printf ("Case %d: ", ++casei);
int flag = false;
if (dist == 0.0)
{
flag == true;
}
else
{
for (int i = 0; i < numIsland; ++i)
{
if (y[i] > dist)
{
flag = true;
break;
}
data[i].beg = x[i]-sqrt(dist*dist-y[i]*y[i]);
data[i].end = x[i]+sqrt(dist*dist-y[i]*y[i]);
}
}
if (flag)
{
printf ("-1\n");
continue;
}
sort (data, data + numIsland, cmp);
memset(visited, 0, sizeof (visited));
//若[jbeg,jend]属于[ibeg,iend]则将visited[i]标记为1
for (int i = 0; i < numIsland; ++i)
{
for (int j = i + 1; j < numIsland; ++j)
{
if (data[j].beg > data[i].end)
break;
if (data[j].end <= data[i].end && data[j].beg <= data[i].end)
{
visited[i] = true;
break;
}
}
}
int i = 0;
while (i < numIsland && visited[i])
{
i++;
}
double end = data[i].end;
int ans = 1;
for (i = i + 1; i < numIsland; ++i)
{
if (visited[i] || data[i].beg <= end)
{
continue;
}
ans += 1;
end = data[i].end;
}
printf ("%d\n", ans);
}
return 0;
}