Greedy problem, so-called greedy is just a thought, no specific solution, just to find the most current solution, can more, and finally try to achieve the overall optimal, which is greed.
The problem is that there is an x-axis as the boundary of the coast, above the sea, there is a island, below the land, there is a radar, scanning range is radius d of the circle, can only be installed on the x-axis, the minimum number of installed radar?
, first of all, as long as the point of A and b what is the meaning of the problem can be quickly answered, A and B point to do the center, D for the radius is just the C point, so a to B is a range, in [A, a] to build a circle can be summed up in C (easy), and then the whole problem becomes a , each point of the two left and right points are calculated, according to the left point to sort, and then update the left and right intervals, see Code (L and R), as long as two intervals have a common range, you can use a radar to cover them up;
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5 using namespacestd;6 7 structpoint{8 DoubleLeft ;9 DoubleRight ;Ten}p[1010]; One A BOOLCMP (Point A, point B) { - return(A.left <b.left); - } the - intMain () - { - intN, D, res, tcase =0; + while(CIN >> N >> d && (n | |d)) { - BOOLFlag =false; + for(inti =0; I < n; i++){ A Doublex, y; atscanf"%LF%LF", &x, &y); - if(Y >d) -Flag =true; - Else{ -P[i].left = X-sqrt (d*d-y*y); -P[i].right = X+sqrt (d*d-y*y); in } - } toprintf"Case %d:", ++tcase); + if(flag) -printf"-1\n"); the Else{ *Sort (p,p+n,cmp); $res =1;Panax Notoginseng DoubleL = p[0].left, R = p[0].right; - for(inti =1; I < n; i++){ the if(R <p[i].left) { +res++; AR =P[i].right; the } + Else if(R >p[i].right) -R =P[i].right; $L =P[i].left; $ } -printf"%d\n", res); - } the } - return 0;Wuyi}
View Code
POJ 1328Radar Installation