Solution radius coverage Problem of greedy algorithm

Source: Internet
Author: User

As shown in the following illustration, the X-axis represents the coastline, the x-axis represents the ocean, the bottom represents the land, and the N-points above the x-axis represent the ship. It is now necessary to install several radars on the coastline, with a maximum coverage radius of D for each radar.
Question: Given the coordinates of the n,d and the ship, the algorithm solves the minimum number of radars required to cover all ships.

Greedy strategy:
Click the x-axis to find the interval that covers the x-axis of each ship's point. Starting from the leftmost point, if the left interval of the next point is larger than the right interval, a new base station is required, and if the next right interval is smaller than the current right interval, the current right interval needs to be updated to be small because all points must be covered.

Proof Ideas:
First, it is proved that the base station is built in the left or right interval which can be covered by the ship.
Assuming that the base station is not built on two interval points, it may not be possible to overwrite the other neighboring points of the ship, so it will not be worse to build at two interval points.

First assume the collection of items s={x1,x2, ... Xn} has been ordered by x-axis from small to large.
And suppose a global optimal solution is: S (i) ={xi1,xi2, ... Xin}. Xi1,xi2, ... The Xin is orderly. For greedy selection, the Xn point is always preferred.
If the Xin = Xn problem has been proven. Because, in our optimal solution s (i), greedy selection has been included. As long as we continue to generalize, Xi (n-1) is Xn-1 ....
If xin! = Xn uses pruning techniques to cut off Xin and paste Xn at this point, the solution will not be worse (because Xn can cover the right-hand side less than Xin). Because the xn can cover both the current point and the most points on the right, our greedy choice should choose it, but here the optimal solution S (i) did not choose it, so we use pruning techniques to add it to S (i), and the S (i) in the Xin removed.
This proves that if the greedy strategy is chosen, the optimal solution is obtained. The correctness of the greedy algorithm is proved.

#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> using namespace std;
struct Node {Double l;//the leftmost coordinate double r;//the most right-to-be-overwritten coordinates};
Node a[1010];
int i,j,k,n,m;
Double d,x,y;
    int ans;//result const double zero=1e-8;//Control Precision BOOL CMP (Node A,node B)//leftmost can be overridden by sorted coordinates, left to right sort {if (A.L&LT;B.L) return 1;
return 0;
    } void Greedy () {double NOW=A[0].R;
    ans++;
            for (int i=1;i<n;i++) {if (A[i].l>now+zero)//The left-most-overridden coordinate of the next point is greater than the current most-right can be overridden by the coordinates {ans++;
        NOW=A[I].R;
        }else if (A[i].r<now+zero)//The left-most-overwritten coordinate of the next point is less than the current most-right can be overridden by the coordinates {NOW=A[I].R;
    }}} int main () {printf ("Input coverage radius d\n");
    cin>>d;
    printf ("Number of input ships n\n");
    cin>>n;

    printf ("Input n-ship coordinates \ n");
        for (int i=0;i<n;i++) {cin>>x>>y;
    Double Len=sqrt (double) (d*d-y*y));//Pythagorean a[i].l=x-len;//calculates the leftmost coordinate to be overwritten a[i].r=x+len;//calculates the right-most-overridden coordinate}Sort (a,a+n,cmp);
    ans=0;
    Greedy ();
    printf ("Place at least%d points on the x-axis to cover all points \ n", ans);
return 0;
 }/* Sample1 input 5 4 3 4 4 3-3 -4-4-3 Output 1 Sample2 input 5 4 3 4 4 3-3 -4-4.1-3 Output 2 */

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.