POJ-1328 radar installation greedy

Source: Internet
Author: User
Tags radar

Calculate the x-axis range of the radar with the Left and Right coverage at each point, and then calculate the number of required circles.

If the center of the next vertex is prior to the right coordinate of the first vertex in the left coordinate of the X axis, the vertex will be overwritten.

The Code is as follows: (C ++ can pass, G ++ runtime error)

#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;int N, R;struct Node{    double l, r;        bool operator < (Node t) const    {        if (r != t.r) {            return t.r > r;        }    }}e[1010];int deal(){    int ans = 1;    double pos = e[1].r;    for (int i = 2; i <= N; ++i) {        if (e[i].l > pos) {            ++ans;            pos = e[i].r;        }    }    return ans;}int main(){    int x, y, flag, ca = 0;    while (scanf("%d %d", &N, &R), N|R) {        flag = 0;        for (int i = 1; i <= N; ++i) {            scanf("%d %d", &x, &y);            if (y > R) {                flag = 1;            }            else {                double dis = sqrt(double(R*R-y*y);                e[i].l = x - dis;                e[i].r = x + dis;            }        }        printf("Case %d: ", ++ca);        if (flag) {            puts("-1");        }        else {            sort(e+1, e+1+N);            int t = deal();            printf("%d\n", t);        }    }    return 0;}

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.