This is a greedy question, but you need to observe the law by yourself. This is not easy and error-prone.
Generally, the online method is to find the interval.
Here is a unique method:
1. sort by X axis size
2. From the leftmost vertex loop, first find the circle of the smallest X axis.
3. Use this circle to determine the number of circles on the right until the next vertex cannot be included. Then, continue step 1 and draw a new circle.
The Code should be clear and intuitive.
The efficiency is O (n). Although there is a nested loop, the subscript is not repeated, and the loop can be done once, so it is O (n ).
# Include <stdio. h> # include <cmath> # include <algorithm> # include <float. h> using namespace STD; const int max_n = 1001; inline float max (float a, float B) {return A> B? A: B;} inline float min (float a, float B) {return a <B? A: B;} struct point {float X, Y; bool operator <(const point & P) const {If (x = P. x) return y> P. y; return x <p. x;} float dist (const point & P) const {float a = (X-P. x); float B = (Y-P. y); Return sqrtf (A * A + B * B);} float dist (const float X1, const float Y1) const {float a = (X-X1 ); float B = (Y-Y1); Return sqrtf (A * A + B * B) ;}}; point PS [max_n]; int calradar (INT N, int D) {sort (Ps, PS + n); float cenx = 0.0f, c Eny = 0.0f; int ans = 0; For (INT I = 0; I <n;) // Add I {ans ++ as needed; float dx = sqrtf (float (D * D)-PS [I]. y * PS [I]. y); cenx = Ps [I]. X + dx; for (I ++; I <n & PS [I]. x <= cenx; I ++) {dx = sqrtf (float (D * D)-PS [I]. y * PS [I]. y); cenx = min (cenx, PS [I]. X + dx) ;}float Dis = 0.0f; For (; I <n; I ++) {Dis = Ps [I]. dist (cenx, ceny); If (DIS> float (D) break;} return ans;} int main () {int N, D, T = 1; while (scanf ("% d ", & N, & D) & (N | D) {float Maxy = flt_min; // This is an error! For (INT I = 0; I <n; I ++) {scanf ("% F", & PS [I]. x, & PS [I]. y); Maxy = max (PS [I]. y, Maxy);} If (Maxy> (float) d) printf ("case % d:-1 \ n", t ++ ); else printf ("case % d: % d \ n", t ++, calradar (n, d) ;}return 0 ;}