Test instructions: Given a number of n points, and a D, it is required to select as few points on the x-axis as possible, so that for each given point, there is a selected point from its Euclidean distance of not more than D.
Analysis: First of all, this is a greedy topic, and is the problem of interval points, what is the interval point, that is, there are n closed intervals on the axis, take as few points as possible, so that each interval has at least one point.
A look is not similar to this question, yes, then where does the interval come from? Oneself make Bai, since said is distance not more than D, meaning is at a given point for the center of the Circle, D as a radius to draw round, on the x-axis of the interval,
So the interval does not have, and then how greedy, is this, the right end of all the interval from small to large order (if the same, then the left endpoint from the big to the small row), then the greedy strategy is to take the last point,
That is, the right end point, think about why, also very good understanding, as far as possible to make many of the interval has this dot. Then the problem is very simple.
The code is as follows:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include < algorithm>using namespace Std;const int maxn = 1E5 + 5;struct node{double L, R; BOOL operator < (const node &P) const{return r < P.R | | (r = = P.R && l > P.L);//Sort}};node a[maxn];int main () {//Freopen ("In.txt", "R", stdin); int n; Double x, Y, L, D; while (scanf ("%lf", &l) = = 1) {//l did not find anything with scanf ("%lf%d", &d, &n); for (int i = 0; i < n; ++i) {scanf ("%lf%lf", &x, &y); A[I].L = X-sqrt (d*d-y*y);//Calculate left Endpoint A[I].R = x + sqrt (d*d-y*y);//Calculate Right end of} sort (A, a+n); int cnt = 1; Double ans = a[0].r;//takes the last point for (int i = 0; i < n; ++i) if (a[i].l > Ans) {//If this is not included, first right end ++cnt; ans = A[I].R; } printf ("%d\n", CNT); } return 0;}
UVa 1615 Highway (greedy, interval point problem)