贪心算法之Radar Installation

 

Radar Installation

Description

Assume thecoasting is an infinite straight line. Land is in one side of coasting, sea inthe other. Each small island is a point locating in the sea side. And any radarinstallation, locating on the coasting, can only cover d distance, so an islandin the sea can be covered by a radius installation, if the distance betweenthem is at most d. 

We use Cartesian coordinate system, defining the coasting is the x-axis. Thesea side is above x-axis, and the land side below. Given the position of eachisland in the sea, and given the distance of the coverage of the radarinstallation, your task is to write a program to find the minimal number ofradar installations to cover all the islands. Note that the position of anisland is represented by its x-y coordinates. 

 
Figure A Sample Input of Radar Installations

 

Input

The input consistsof several test cases. The first line of each case contains two integers n(1<=n<=1000) and d, where n is the number of islands in the sea and d isthe distance of coverage of the radar installation. This is followed by n lineseach containing two integers representing the coordinate of the position ofeach island. Then a blank line follows to separate the cases. 

The input is terminated by a line containing pair of zeros 

Output

For each test caseoutput one line consisting of the test case number followed by the minimalnumber of radar installations needed. "-1" installation means nosolution for that case.

Sample Input

3 2

1 2

-3 1

2 1

 

1 2

0 2

 

0 0

Sample Output

Case 1: 2

Case 2: 1

 

 

题意解读:第一行输入(x,y)坐标对数和雷达举例d

接下来n行分别输入坐标对,然后求得覆盖所有点最少的雷达数

 

需要注意的是:

1、  如果n=0,最后的结果为0

2、  如果出现d<=0或者某个点的纵坐标<0或者纵坐标大于d,则最后的结果为-1

3、  在两次输入之间存在着一个空格行,用gets()

 

解题思路:

显然,对于不满足以上三点的(即通常情况下的坐标来说)情况,根据给定的d值以及坐标点,我们可以求得在x轴的一个范围,即在这个范围之内部署雷达,都能搜到该岛屿。其图例如下:

然后我们可以分别求得n个结点在x轴上的范围区间,然后按照左区间排序,重叠的部分可以用一个雷达搜索到。(这里就用到了贪心思想,选择雷达覆盖数目最多的一个),需要注意的是在选择雷达区间时得更新雷达的右区间。

其具体代码如下:

// 1328.cpp : 定义控制台应用程序的入口点。

//

 

#include "stdafx.h"

 

 

#include<iostream>

#include<math.h>

#include<algorithm>

using namespace std;

 

#define maxn 1005

 

int n;

int d;

struct Node

{

         intx;

         inty;

};

Node node[maxn];

struct Key

{

         doubleleft;

         doubleright;

};

Key key[maxn];

 

int result();

bool sorted(Key key1,Key key2);

 

int main()

{

         intnumcase=1;

         cin>>n>>d;

         intmincount;

         boolflag=true;

   char s[10]; 

         while((n!=0)||(d!=0))

         {

                   flag=true;

                   if((n==0))

                   {

                            mincount=0;

                   }

                   else

                   {

                            if(d<=0)

                            {

                                     mincount=-1;

                            }

                            for(inti=1;i<=n;i++)

                            {

                                     cin>>node[i].x>>node[i].y;

                                     if(node[i].y>d||node[i].y<0)

                                               flag=false;

                            }

                   }

                  

                   if((n!=0)&&(d!=0))

                   {

                            if(d>0)

                            {

                                     if(flag)

                                               mincount=result();

                                     else

                                               mincount=-1;

                            }

                            else

                                     mincount=-1;

                   }

                   //cout<<"Case"<<numcase<<": "<<mincount<<endl;

                   printf("Case%d: %d\n",numcase,mincount);

                   gets(s);

                   numcase+=1;

                   cin>>n>>d;

         }

         return0;

}

int result()

{

         doubledd=d*d;

         for(inti=1;i<=n;i++)

         {

                   key[i].left=(double)node[i].x-sqrt(dd-node[i].y*node[i].y);

                   key[i].right=(double)node[i].x+sqrt(dd-node[i].y*node[i].y);

         }

         sort(key+1,key+n+1,sorted);

         intmincount=1;

         Keykeytemp=key[1];

         doubleright;

         right=keytemp.right;

         for(inti=2;i<=n;i++)

         {

                   if(key[i].left<=right)

                   {

                            if(key[i].right<right)

                                     right=key[i].right;

                            continue;

                   }

                   mincount+=1;

                   keytemp=key[i];

                   right=keytemp.right;

 

         }

         returnmincount;

}

bool sorted(Key key1,Key key2)

{

         if(key1.left<=key2.left)

                   returntrue;

         else

                   returnfalse;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值