There are many islands in the sea. Now we need to build a radar on the coast, but the coverage radius of each radar is only D. In order to make every island covered, we need the least radar. (Create a coordinate system. The coast is the X axis, and the island is given in the form of coordinates.
Solution: First, find the coverage range of each island. That is, when the island is covered, the leftmost position and rightmost position that the radar can establish. Sort the leftmost position of each island in ascending order, and then solve it with greed.
# Include <cmath> # include <algorithm> # include <cstdlib> # include <iostream> using namespace STD; struct P {double left; double right;} POS [1001]; int CMP (const void * a, const void * B) {P * C, * D; C = (p *) A, D = (p *) B; return C-> left> D-> left? 1:-1;} int main () {int N, res, I, test = 0; double D, current, X, Y; while (CIN> N> D & N & D) {res = 0; for (I = 0; I <n; I ++) {CIN> x> Y; If (Y> d) RES =-1; else {pos [I]. left = x-SQRT (D * D-y * Y); POS [I]. right = x + SQRT (D * D-y * Y);} If (RES =-1) {cout <"case" <++ test <":-1" <Endl; continue;} qsort (Pos, N, sizeof (P), CMP ); current = POS [0]. right; Res = 1; for (I = 1; I <n; I ++) {/* When the leftmost position of island I is greater than the rightmost position of the previous Island, it indicates that the previous Island cannot share radar with the island behind it. Therefore, a radar */If (Pos [I] needs to be built for the previous Island. left> current) {res ++; current = POS [I]. right;} else {/* When island I is within the coverage of the previous Island, current should be changed to the right side of island I */If (Pos [I]. right <Current) Current = POS [I]. right ;}} cout <"case" <++ test <":" <res <Endl ;}return 0 ;}