UVa 1615 Highway (greedy, interval problem)

Source: Internet
Author: User

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 &AMP;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.