Radar Installation
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 48402 |
|
Accepted: 10768 |
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
题意:
有一个平面坐标,当前假设 x 轴上方为大海,在海上有 n 座岛屿,需要在海岸线上( x 轴)建造一个个雷达去覆盖海上的岛屿,而且雷达都是统一的,具有相同的覆盖面积,其半径为 d,要求输出能覆盖所有岛屿的最小雷达数,未满足要求时输出 -1 。
思路:
想法很简单,就是贪心的思路,一个圆尽量覆盖更多的点(岛屿)。
只需要将点的坐标按 x 进行排序,先处理左边的点,依次往右边处理。
有两个地方需要注意一下:
1、如下图:
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
// 岛屿对象
struct coordinate
{
// 坐标、与 X 轴相交线段的起/终点
double x, y;
double s, e;
bool operator<(const coordinate &o) const {
return e < o.e;
}
} island[1001];
int solve(int n, int d)
{
// 计算相交线段起/终点
for (int i = 0; i < n; ++i)
{
double t = sqrt(d - island[i].y * island[i].y);
island[i].s = island[i].x - t;//左侧
island[i].e = island[i].x + t;//右侧
}
// 依线段终点排序
sort(island, island + n);
// 最少雷达数量(特殊情况已在输入时排除掉,
// 程序执行到这里时,至少存在一个合法岛屿,
// 故 ans 初始为 1)
int ans = 1;
double r = island[0].e;
// 从第二座岛屿开始扫描,如前一雷达设置点
// 覆盖不到,则雷达数加 1
for (int i = 1; i < n; ++i) {
if (island[i].s > r) {
++ans;
r = island[i].e;
}
}
return ans;
}
int main()
{
int c = 1;
int n, d;
while (scanf("%d %d", &n, &d) && (n || d))
{
bool np = false;
for (int i = 0; i < n; ++i)
{
scanf("%lf %lf", &island[i].x, &island[i].y);
if (island[i].y > d) {
np = true;
}
}
if (np)
printf("Case %d: %d\n", c, -1);
else
// 直接传入半径平方,减少重复计算
printf("Case %d: %d\n", c, solve(n, d * d));
++c;
}
return 0;
}