传送门:POJ 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.
Input
The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each island. Then a blank line follows to separate the cases.
The input is terminated by a line containing pair of zeros
Output
For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. “-1” installation means no solution for that case.
Sample Input
3 2
1 2
-3 1
2 1
1 2
0 2
0 0
Sample Output
Case 1: 2
Case 2: 1
题意:
题目大意是假设海岸线是一条无限延伸的直线。陆地在海岸线的一侧,而海洋在另一侧。每一个小的岛屿是海洋上的一个点。雷达坐落于海岸线上,只能覆盖d距离,所以如果小岛能够被覆盖到的话,它们之间的距离最多为d。
题目要求计算出能够覆盖给出的所有岛屿的最少雷达数目。
思路:
这是一道区域覆盖问题。我们由数学知识可知,对于每个小岛,我们可以计算出一个雷达可以覆盖它的所在位置的区间。
如图:
由图可知:一个岛屿(x,y),为了覆盖它,雷达可放置的区域为(x-r,x+r).
此时问题就可转化为以尽可能少的点覆盖这些区间,与活动时间安排问题有些相似。
我们先将这些区间按左端由小到大排序,初始时需要一个点。如果两个区间相交而不重合,我们需要的雷达数不变;如果一个区间完全包含于另外一个区间,我们需要更新区间的右端点,取小区间;如果两个区间不相交,我们需要增加雷达数并更新右端点。
代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=11000;
int n,d;
struct Node
{
double x,y;
}points[N];//记录区间
bool cmp(Node a,Node b)
{
return a.x<b.x;
}
int main()
{
int x,y;
int ca=0;
while(scanf("%d%d",&n,&d)&&n&&d)
{
int ans=1;
for(int i=0;i<n;i++)
{
scanf("%d%d",&x,&y);
if(y>d) ans=-1;//一旦有大于搜索半径的岛屿,就没有解决方案
double r=sqrt(d*d-y*y);
points[i].x=x-r;
points[i].y=x+r;
}
if(ans!=-1)
{
sort(points,points+n,cmp);
double s=points[0].y;
for(int i=1;i<n;i++)
{
if(points[i].x>s)
{
ans++;
s=points[i].y;
}
else if(points[i].y<s)
{
s=points[i].y;
}
}
}
printf("Case %d: %d\n",++ca,ans);
printf("\n");
}
return 0;
}