Radar
时间限制:1000 ms | 内存限制:65535 KB难度:3
描述
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.
转存失败重新上传取消
输入
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
输出
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.
样例输入
3 2 1 2 -3 1 2 1 1 2 0 2 0 0
样例输出
Case 1: 2 Case 2: 1
来源
上传者
ctest 大意: 海上有n个海岛,图中y上半轴为海,下半轴为陆地。现布置一些雷达在x轴上,作用半径为d,求覆盖全部海岛的最少雷达数。输入第一行为n,d;以后的n行为n个海岛的坐标x,y;以0,0代表结束,如果不能完全覆盖,输出-1. 思路: 如果雷达可以覆盖到海岛,那么以海岛为圆心,以d为半径画圆在x轴必相交(最坏也是相切),那么就有一段区间,在这个区间内雷达在哪都能覆盖这个海岛,如果两个海岛作圆,如果区间有重叠,在重叠处放置一个雷达就可覆盖两个海岛,以此类推。那么就转化成了贪心算法中区间找点的问题,将每个海岛在x轴形成的坐标区间,保存到数组,并排序,再判断。
#include<stdio.h> #include<stdlib.h> #include<math.h> struct fun { double top;//左坐标 double last;//右坐标 }s[1000]; int cmp(const void*a,const void*b) { return (*(fun*)a).last>(*(fun*)b).last?1:-1; } int main() { int k=0; double n,r; while(~scanf("%lf %lf",&n,&r)) { int flag=1; if(n==0&&r==0) break; double x,y; for(int i=0;i<n;i++) { scanf("%lf %lf",&x,&y); if(y>r)//海岛距离太远,雷达半径不够,不能覆盖 { flag=0; break; } s[i].top = x-sqrt(r*r-y*y); s[i].last = x+sqrt(r*r-y*y); } qsort(s,n,sizeof(s[0]),cmp);//以右坐标从小到大排序 int sum=1; double temp=s[0].last ;//左边第一个肯定放置一个, for(int i=1;i<n;i++)//从第二个开始判断是否有重叠 { if(s[i].top > temp)//左坐标大于上一个右坐标 ,不重叠,要放置雷达 { sum++; temp=s[i].last ; } } if(flag==1) printf("Case %d: %d\n",++k,sum); else printf("-1\n"); } return 0; }