Radar Installation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 64121 | Accepted: 14418 |
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
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
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
区间选点(转化思想):
<1>: 对左端点排序(小 → 大), 左端点相同,(小→大)排右端点;
<2>:对于num[0],在右端点放雷达,如果下一个区间左端点>现在右端点, 雷达shu+1, 如果左端点<右端点: 如果下一区间右端点<现在右端点, 更新雷达到下一区间右端点;
<1>: 对左端点排序(小 → 大), 左端点相同,(小→大)排右端点;
<2>:对于num[0],在右端点放雷达,如果下一个区间左端点>现在右端点, 雷达shu+1, 如果左端点<右端点: 如果下一区间右端点<现在右端点, 更新雷达到下一区间右端点;
1 #include <cmath> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 struct island 7 { 8 double l, r; 9 } num[1010]; 10 11 bool cmp(island l, island r) 12 { 13 if(l.l == r.l) 14 return l.r < r.r; 15 return l.l < r.l; 16 } 17 18 int main() 19 { 20 double r, x, y; int i, m, t=1; 21 while(~scanf("%d %lf", &m, &r)) 22 { 23 int flag = 0; 24 if(m == 0 && r == 0) 25 break; 26 for(i=0; i<m; i++) 27 { 28 scanf("%lf %lf", &x, &y); 29 if(y > r) 30 {flag = 1; continue; } 31 num[i].l = x - sqrt(r*r - y*y); //转化为区间问题; 32 num[i].r = x + sqrt(r*r - y*y); 33 } 34 if(flag) 35 { 36 printf("Case %d: -1 ",t++); 37 continue; 38 } 39 sort(num, num+m, cmp); 40 double temp = num[0].r; int total = 1; 41 for(i=1; i<m; i++) 42 { 43 if(num[i].l > temp){ 44 total++; 45 temp = num[i].r; 46 } 47 48 if(num[i].l <= temp) 49 { 50 if(num[i].r < temp) 51 temp = num[i].r; 52 } 53 } 54 printf("Case %d: %d ", t++, total); 55 } 56 return 0; 57 }