Description Assume the coasting isAn infinite straight line. Land is inchOne side of coasting, seainchThe other. Each of small island isA point locatinginchThe sea side. and any radar installation, locating on the coasting, can only cover D distanceinchThe sea can be covered by a RADIUS installation,ifThe distance between them isAt the most D. We use Cartesian coordinate system, defining the coasting isThe x-axis. The sea side isAbove x-axis, and the land side below. Given the position of each islandinchThe sea, and given the distance of the coverage of the radar installation, your task isTo write a program to find the minimal number of radar installations to cover all the islands. Note that the position of a island isrepresented by its X-y coordinates. Figure A Sample Input of Radar installations
Input case contains integers n (1<=n<= is is
Output Case Case " -1 " for case.
Sample Input 3 2 1 2-31211202 0 0
Sample Output 1 2 21
Source Beijing 2002 |
Greedy.
The first idea:
The ABCD in the figure is the location of the island. Assuming that the radius of the subject is 2 (conforming to the coordinate system), then the a point coordinate is () and so on.
In the title, record the coordinates of each point and add a new marker variable to mark whether it has been accessed.
First, A is the center, R is the radius of the circle, the x-axis and E1 (right) point, make the green dashed circle. Then take the E1 as the center, the radius is R do circle. Look at the next point (B) Whether it is in the circle. If so, then change the dot tag variable to true, then the next point (C), and if not, add a new radar.
Later, the idea was changed to store the intersection of the red circle and the x-axis in the graph.
Still the original greedy ideas, still sorted first, but the sorting criterion is the order of the red circle and the left intersection of the x-axis.
, if the left intersection (J1 Point) of the B point circle (and so called) is to the left of the right intersection of the point Circle (E1), then the B point must be covered within the a point circle. Then, if the left intersection of the D-point circle (not shown in the figure) is right at the right intersection of the point circle (not marked), the D point is not in the A-point circle.
Therefore, there is the following code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5#include <stdlib.h>6#include <cmath>7 using namespacestd;8 structNode9 {Ten DoubleLeft,right; One}p[1006]; A BOOLCMP (Node a,node B) - { - returna.left<B.left; the } - intMain () - { - intN; + DoubleR; - intAc=0; + while(SCANF ("%D%LF", &n,&r) = =2&& (n | |R)) A { at intf=1; - for(intI=0; i<n;i++) - { - Doubleb; -scanf"%LF%LF",&a,&b); - if(b>R) in { -f=0; to } + Else - { theP[I].LEFT=A-SQRT (r*r-b*b); *P[I].RIGHT=A+SQRT (r*r-b*C); $ }Panax Notoginseng } -printf"Case %d:",++AC); the if(f==0) + { Aprintf"-1\n"); the Continue; + } -Sort (p,p+n,cmp); $ intans=1; $Node tmp=p[0]; - for(intI=1; i<n;i++) - { the if(p[i].left>tmp.right) - {Wuyians++; thetmp=P[i]; - } Wu Else if(p[i].right<tmp.right) - { Abouttmp=P[i]; $ } - } -printf"%d\n", ans); - A } + return 0; the}
View Code
POJ 1328 Radar installation (greedy)