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.
Input
The input consists of several test cases. The first line of each case contains two integers n (1n1000) 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
题意:
给出一个坐标轴, y的正半轴是海, 负半轴是大陆, x轴是海岸线;
然后在海上有很多海岛, 需要雷达监控, 现在的问题是, 至少需要多少个雷达, 能全部覆盖这些海岛?
做法:
对于每一个海岛的坐标, 我们可以在海岸上得到最左和最右的两个检测点, 若海岛到海岸的距离为雷达覆盖半径, 那么最左和最右两个检测点重合~
我们求出每个海岛对应的最左和最右的两个检测点, 然后按最右点升序排序这些点.
排好后, 我们针对每个海岛, 都与后面的海岛比较, 若下一个海岛对应的最左检测点小于当前海岛的最右检测点, 那么在这个最左和最右这个区间, 必有一点, 可同时覆盖着两个岛!
AC代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct Point{
double x;
double y;
double l;
double r;
int mark;
}point[1234];
int cmp(Point a, Point b) {
if(a.r != b.r)
return a.r < b.r;
else
return a.l > b.l;
}
int main() {
int n, d;
int cas = 0;
while(scanf("%d %d", &n, &d) != EOF) {
if(n == 0 && d == 0)
break;
int i, j;
int ans = 0;
int flag = 1;
memset(point, 0, sizeof(point));
for(i = 0; i < n; i++) {
scanf("%lf %lf", &point[i].x, &point[i].y);
if(point[i].y > d)
flag = 0;
point[i].l = point[i].x - sqrt(d * d - point[i].y * point[i].y);
point[i].r = point[i].x + sqrt(d * d - point[i].y * point[i].y);
}
printf("Case %d: ", ++cas);
sort(point, point + n, cmp);
if(flag) {
for(i = 0; i < n; i++) {
if(!point[i].mark) {
double tx = point[i].r;
for(j = i; j < n; j++) {
if(point[j].l <= tx && !point[j].mark)
point[j].mark = 1;
}
ans ++;
}
}
printf("%d\n", ans);
}
else
printf("-1\n");
}
return 0;
}