贪心算法解决雷达站建站问题
admin
2024-03-18 11:40:48

问题描述:

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)

相关内容

热门资讯

两周爆出第三个漏洞!Linux... 快科技5月14日消息,安全研究团队V12 Security近日公开披露Linux内核本地提权漏洞“F...
“按AI建议退改机票亏钱”引热... 近日,有网友在社交平台发文称,“因相信豆包给出的机票退改签建议后亏损600元”,相关话题冲上热搜。5...
被网友点赞的酒店服务!暖心文旅... 最近 有网友在小红书分享了来三明的暖心体验 入住酒店时 大堂里摆放的文旅资料 让她直呼 “被三明文旅...
“赌王之子”,在三亚豪赌电竞酒... 阳光、沙滩、海浪、仙人掌,还有——一间电竞房?这画面,说不准还真的在三亚集齐了。 就在前几天,澳门赌...
线上冠名“八大局店”,线下隔了... 如今,淄博八大局便民市场、海岱楼凭借浓郁的烟火气和深厚的文化气息,成为外地游客来淄打卡的核心地标。然...