• 贪心算法解决雷达站建站问题


    问题描述:

    Assume the coasting is an infinite straight line. Land is in one side of coasting,
    sea in the other. Each small island is a point locating in the sea side. And any
    radar installation, locating on the coasting, can only cover d distance, so an
    island in the sea can be covered by a radius installation, if the distance between
    them is at most d.
    We use Cartesian coordinate system, defining the coasting is the x-axis. The
    sea side is above x-axis, and the land side below. Given the position of each
    island in the sea, and given the distance of the coverage of the radar installation,
    your task is to write a program to find the minimal number of radar installations
    to cover all the islands. Note that the position of an island is represented by its
    x-y coordinates.

    解题:

            因为所有的雷达站都是在X坐标轴上,而所有的岛都是在X轴的上方。因此,若要找出雷达站的数量,首先应该弄清楚雷达站的坐标。

         (1)对于坐标为(x,y)的小岛,若要使位于x轴上的雷达站覆盖到它,则以此小岛为圆心,以雷达覆盖半径为半径画圆,此圆与x轴的交界区间即为雷达的部署区间。即为 ( x-sprt(d*d-y*y) ,x+sprt(d*d-y*y) )。设小岛i的左右区间点分别放在island[i][0],island[i][1]。

      (2)以(1)中的方法找出每个小岛如果被覆盖,雷达站安置的区间。并以所有区间的右区间点从小到大进行排列。即依据island[i][1]进行排列。

      (3)首先将island[0][1]设为第一个雷达站点,并将所有左区间点island[j][0]小于island[0][1]的小岛去掉,其右区间点island[j][1]随之在数组中去掉。

      (4)对于剩下的所有island[k][0]重复(3)的过程,直到没有小岛为止。没重复进行一次(3)的操作,说明雷达站建起一座。

    pseudo-code

        i=num(island);

       while(i--)

       {

         island[i][0]=land[i].x-sprt(d*d-land[i].y*land[i].y);

         island[i][1]=land[i].x+sprt(d*d-land[i].y*land[i].y);

        }

       sort(island);//依据island[i][1]进行排序;

       for(k=0;k

       {

            for(j=k+1;j

             {

               if(island[j][0]

                delete island[j];//如果小于即改点已被雷达覆盖;

              }

             radar_count++;

             sort(island);//继续对剩下的点排序;

             找出新排序的第一个点作为新k;

        }

        return radar_count;

    complexity of your algorithm:

       由于在for循环里面嵌套一个排序算法,因此时间复杂度为(n^2*logn)

  • 相关阅读:
    MySQL和SQLserver中group by的区别
    spring-初识spring
    Kafka 集群安装
    SwiftUI 中为什么应该经常用子视图替换父视图中的大段内容?
    P03 MySQL 数据类型详解
    循环购:复购越多分红越多,让你的用户活起来
    敲代码之余的表情包
    PostgreSQL下载和安装教程
    【C++】-- AVL树详解
    ElasticSearch之结构化搜索
  • 原文地址:https://blog.csdn.net/G11176593/article/details/128175789