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
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 轴。海洋的一侧位于 x 轴上方,陆地的一侧位于下方。给定海洋中每个小岛的位置,并给定雷达安装的覆盖距离,您的任务是写一个程序,找出雷达安装的最少数量,使得所有的小岛都被覆盖。注意:小岛的位置以它的 x-y 坐标表示。
题解:这道题乍一看没有思路,但是如果不从雷达考虑,从小岛考虑,那么每个小岛都有一个对应的覆盖区间,只有区间内的点才能覆盖这个岛,所以问题变成了区间选点
只是要注意,精度是double的
代码:
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; struct node { double l,r; } a[1010]; int x[1010],y[1010]; int n,d,cnt,t=0; bool cmp(node a,node b) { return a.l<b.l; } int main() { while(scanf("%d%d",&n,&d)!=EOF&&n) { t++; int flag=0; cnt=1; if(d<=0) { flag=1; } for(int i=1; i<=n; i++) { scanf("%d%d",&x[i],&y[i]); if(y[i]>d||y[i]<0) { flag=1; } double dr=sqrt((double)d*d-y[i]*y[i]); a[i].l=x[i]-dr; a[i].r=x[i]+dr; } if(flag==1) { printf("Case %d: -1 ",t); continue; } sort(a+1,a+n+1,cmp); double lim=a[1].r; for(int i=2; i<=n; i++) { if(a[i].l>lim) { lim=a[i].r; cnt++; } else { if(a[i].r<lim) { lim=a[i].r; } } } printf("Case %d: %d ",t,cnt); } }