Radar Installation
Time Limit: 1000 MS Memory Limit: 10000 K
Total Submit: 2704 Accepted: 564
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 abve 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 21 2-3 12 11 20 20 0
Sample Output
Case 1: 2Case 2: 1
Source
Beijing 2002
Algorithm: Greedy
Step 1: input data if the y coordinate of a vertex is greater than d, directly output-1 to generate the corresponding interval (this Island is visible if any Radar is placed in a certain interval)
Step 2: sort by left endpoint (if the left endpoint is equal, it must be sorted by Reverse Order on the right. The reason is that the large range must be put before the filtering out of the contained content) note that it is best to use floating-point judgment rules.
Step 3: Use Stack to pre-process these intervals and identify the included intervals.
Step 4: select the right vertex greedy
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
# Include <math. h>
Using namespace std;
Const int n= 1000;
Const double EPS = 1e-7;
Struct E {double a, B;} e [N];
Int n, d;
Bool ava [N];
Double p [N];
Inline int dblcmp (double a, double B ){
If (fabs (a-B) <EPS) return 0;
If (a-B> 0) return 1;
Return-1;
}
Bool operator <(const E & a, const E & B ){
If (dblcmp (a. a, B. a) = 0)
Return dblcmp (a. B, B. B) = 1;
Return dblcmp (a. a, B. a) =-1;
}
Int main (){
Int I, j, x, y;
Int tc = 0;
While (scanf ("% d", & n, & d), n + d ){
Tc ++;
Int OK = 1;
For (I = 0; I <n; ++ I ){
Scanf ("% d", & x, & y );
If (y> d) OK = 0;
Double offset = sqrt (d * d-y * y );
E [I]. a = x-offset, e [I]. B = x + offset;
Ava [I] = 1;
}
If (! OK) {printf ("Case % d:-1 \ n", tc); continue ;}
Sort (e, e + n );
Int stack [N], top = 0;
Stack [top ++] = 0;
For (I = 1; I <n; ++ I ){
While (top> 0 & dblcmp (e [stack [top-1]. B, e [I]. B )! =-1 ){
Ava [stack [-- top] = 0;
}
Stack [top ++] = I;
}
For (I = 0 ;! Ava [I]; ++ I );
P [I] = e [I]. B;
Int cnt = 1;
For (I = I + 1; I <n; ++ I) if (ava [I]) {
For (j = I-1 ;! Ava [j]; j --);
If (p [j]> = e [I]. a) p [I] = p [j];
Else {p [I] = e [I]. B; cnt ++ ;}
}
Printf ("Case % d: % d \ n", tc, cnt );
}
Return 0;
}