POJ1328 Radar Installation Greedy __ Greedy

Source: Internet
Author: User
Tags radar

The main topic: There are N islands in the sea, now want to install radar on the coastline to detect these islands (the coastline is equivalent to the x axis, the island is above the x-axis), the radar detection range is a radius d circle, known N Island coordinates, ask you at least how much radar to detect all the islands If the entire island cannot be detected, the output "-1".


Analysis:

Obviously, if the input of the island's y-coordinates is greater than the radar detection range, then we can directly determine the impossible to detect all the islands (because the radar is always on the coastline).

As for all the islands that can be detected, we know that each given the coordinates of each island, we can all find so two radars on the coastline, so that the island happens to be on the left and right edges of the two radars ' range of detection, and we record these two marginal values as the interval values that can be detected on the coastline. , so that after the coordinates of the N islands are determined, we have identified n interval values to detect the islands, and then we have to filter out the effective interval values, that is, the minimum number of radars.

Implementation code:

#include <cstdio> #include <algorithm> #include <iostream> #include <cmath> using namespace std; typedef struct Node {double left,right;}
Interval
Interval ans[1010];
    int cmp (const interval &a,const interval &b) {return a.left<b.left;} int main () {int n,x,y;
    Double D;
    BOOL Flag;
    int t=1;
        while (scanf ("%d%lf", &n,&d)) {if (n==0&&d==0) break;
        Flag=true;
            for (int i=0;i<n;i++) {scanf ("%d%d", &x,&y);
            if (y>d) Flag=false;
            ANS[I].LEFT=X*1.0-SQRT (D*d-y*y);
        ANS[I].RIGHT=X*1.0+SQRT (D*d-y*y);
        printf ("Case%d:", t++);
        if (!flag) puts ("-1");
            else {sort (ans,ans+n,cmp);
            int cnt=1;
            Interval Tmp=ans[0];
                  for (int i=1;i<n;i++) if (ans[i].left>tmp.right) {cnt++;
Tmp=ans[i];              else if (ans[i].right<tmp.right) tmp=ans[i];
        printf ("%d\n", CNT);
} 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.