Radar Installation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 127683 | Accepted: 28056 |
Description
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.
Figure A Sample Input of Radar Installations
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
Source
下面是个人对网上题解的理解,但是还是有困惑的地方。
题目大意:在一个直角坐标系中,x轴上方有一些点(代表小岛),需要在x轴上选择一些点(代表雷达),而点的覆盖范围为半径为d的圆,求覆盖所有小岛所需要的最少雷达数。
思路:首先我们如果从x上的点的角度出发搜索的话,那么每个点的坐标可以无限小(0.1,0.11,0.111111111..等),那么我们需要转换一下对象,从小岛入手。
贪心
由于半径已知,小岛坐标已知,如果我们以小岛为圆心做圆,那么在该圆范围内的所有点都可以作为雷达点类覆盖这个小岛。
由于雷达只能在x轴上,因此我们可以得到这个圆与x轴上的两个交点left、right,那么在[left, right]区间内的所有点都可以作为雷达点。
通过上面的做法,我们可以得到(单独)对于每一个小岛可能的雷达点坐标区间。
现在我们来考虑一个小问题:
让我们假设两个岛为A、B,上述的雷达点的区间为[lA, rA], [lB, rB],接下来我们来考虑他们的位置关系:
1) 如下图所示, A和B的雷达点区间不相交,那么在A的雷达点区间选定的雷达是无论如何都覆盖不到B的,这个时候如果A已经有一个雷达点了,那么B还需要一个
这个时候的条件是:rB<lA
2) 如下如所示,A的雷达点区间包含了B的雷达点区间,那么A中选定的雷达点一定可以覆盖到B,这个时候B就不需要再重新用一个新的雷达了,和A公用就好,都能覆盖到,这个时候rB<=rA
(???。。)
接下来我们需要做什么呢?上面那两张图中,我们的区间在我们眼中是排列好了的,可以方便我们去比较,但是程序并不像我们这么强大,那么我们也可以把所有的区间排列好。
我们按照区间的左起点,对区间进行排序,左起点小的在前,排序后我们可以扫描区间。扫描开始前可以以第一个区间为确定安装雷达的初始区间(也就是图中的A区间),我们从第二个区间开始与前面确定的区间比较,当发现上图1的情况,我们就需要新建一个雷达点,并把当前的区间作为确定安装雷达的区间;当发现图2情况时,我们不需要新建雷达,只需要将确定安装雷达的区间更新为当前区间即可。(???。。)
接下来是我的一些疑惑,也就是上面出现(???。。)的地方。
细心的各位可能会发现我上面只画了两种情况的图,相交的图没有画。。
比如说上图,假设这就是我们全部的测试数据,那么按照上面的题解就需要两个雷达,但是在区间[lB, rA]中选择一个点即可覆盖A和B,一个雷达就足够了。但是假如我们在相交时即判断不需要新的雷达时,也就是只有当rA<lB时我们才重新更新确定放置雷达的区间时,交上去是WA。。。
还有一个地方就是:
、
这是从上面变种而来,也就是A的区间包含了B,C的区间,他们也拥有共同的交集,按理来说,一个雷达便可以了。
还是贴一下代码把。。。太菜了。。解决不了。。
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int Max = 1005;
int n, d, x, y;
struct point
{
double left, right;
bool operator < (const point& p) const // 左起点小的在前
{
return left < p.left;
}
}islands[Max];
int main()
{
int c = 1;
while(~scanf("%d %d", &n, &d) && (n || d))
{
bool flag = true;
for(int i = 0; i < n; i++)
{
scanf("%d %d", &x, &y);
if(y > d) flag = false;
double l = sqrt(d * d * 1.0 - y * y); // 计算得到雷达点的区间
islands[i].left = x - l;
islands[i].right = x + l;
}
if(!flag)
{
printf("Case %d: -1\n", c++);
continue;
}
sort(islands, islands + n); // 排序
int r = 0, ans = 1; // r代表上一个确定的雷达点所在的区间,ans代表所需雷达点数目
for(int i = 1; i < n; i++) // 从第二个开始
{
if(islands[i].right <= islands[r].right) r = i;
else if(islands[i].left > islands[r].right)
{
ans++;
r = i;
}
}
printf("Case %d: %d\n", c++, ans);
}
return 0;
}