Radar Installation
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 |
提示
题意:
假设一条无线长直线分为两块区域,一部分是陆地,另一部分是海。每一个小岛在海中为一点,而雷达装置放置在陆地和海的交界线上,覆盖半径为d。
我们用直角坐标系表示,x轴下面的是陆地,x轴上方的是海,给出小岛的坐标和雷达的覆盖半径,求出用最少雷达装置能完全覆盖全部的小岛。
思路:
这题话说是贪心,然而如何去贪却是大问题,以前做的题都是很直接的,现在需要转化一下思路。
直接上贪心显然不行,转化一下问题,我们以小岛做半径为d的园,那么直线就会成为圆的弦,那条弦就是雷达能覆盖这座小岛的安装区间,这样就转化为在这些区间上用最少的点才能保证每一区间上都有一个点。
示例程序
Source Code
Problem: 1328 Code Length: 1078B
Memory: 424K Time: 16MS
Language: G++ Result: Accepted
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
struct num
{
double s,t;
}a[1000];
int cmp(struct num x,struct num y)
{
if(x.t<y.t)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int n,d,i,k,i1,x,y,num;
scanf("%d %d",&n,&d);
for(i=1;n!=0||d!=0;i++)
{
k=0;
num=n; //假设每一个都要装雷达
for(i1=0;n>i1;i1++)
{
scanf("%d %d",&x,&y);
if(y>d)
{
k=1; //如果有小岛不能覆盖作记录
}
a[i1].s=x-sqrt(d*d-y*y); //区间的起点
a[i1].t=x+sqrt(d*d-y*y); //区间的终点
}
printf("Case %d: ",i);
if(k==0)
{
sort(a,a+n,cmp); //以终点做递增排序
for(i1=1;n>i1;i1++)
{
if(a[k].t>=a[i1].s)
{
num--; //如果有区间重叠把点放在重叠部分就可以减少点的数量
}
else
{
k=i1; //这和活动选择有点不同,因为要保证每一个区间要有共同的重叠部分
}
}
printf("%d\n",num);
}
else
{
printf("-1\n");
}
scanf("%d %d",&n,&d);
}
return 0;
}