The problem is greedy method, but it is necessary to observe the law, which is not easy, it is easy to make mistakes.
The general online approach is to find the way to the interval.
Here's a unique approach:
1 Sort by x-axis size
2 from the leftmost point loop, first find the smallest x-axis circle
3 Judging by this circle can include the number of circles on the right, until the next point can not be included, then proceed to the 2nd step and draw a new circle.
Look at the code, it should be very clear and intuitive.
Efficiency is O (n), although there is a nested loop, but the subscript is not duplicated, once again the loop can be, therefore 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, ceny = 0.0f;
int ans = 0;
for (int i = 0; i < n;)//Increase I {ans++ as appropriate;
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%d", &n, &d) && (n | | d)) {Float Maxy = flt_min;//This put out, error.
for (int i = 0; i < n; i++) {scanf ("%f%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; }