POJ1328:Radar Installation

来源:互联网 发布:编程九九乘法表 编辑:程序博客网 时间:2024/05/16 06:51

点击打开题目链接


Radar Installation

Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 41891 Accepted: 9270

Description

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. 
 
Figure A Sample Input of Radar Installations


Input

The input consists of 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 is the distance of coverage of the radar installation. This is followed by n lines each containing two integers representing the coordinate of the position of each 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 case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.

Sample Input

3 21 2-3 12 11 20 20 0

Sample Output

Case 1: 2Case 2: 1

Source

Beijing 2002

=====================================题目大意=====================================


以海岸线为X轴(上方为海下方为岸)建立直角坐标系,给出海中N个岛屿(看做是点)的X-Y坐标。

求解至少需要在海岸线上安装多少个覆盖半径为R的雷达装置才能覆盖所有岛屿。


=====================================算法分析=====================================


贪心算法。


1、对每个岛屿:


   如果:岛屿纵坐标大于雷达覆盖半径,则不存在可以覆盖该岛屿的雷达放置位置(此时可直接得出答案为-1)。

   否则:以岛屿为圆心,雷达覆盖半径为半径做圆,交海岸线于(A,0),(B,0)(A<=B)两点,则区间[A,B]为可以覆盖该岛屿的雷达

         放置区间。


2、题目转换为:


   选取尽可能少的点(即雷达放置位置)使得每个上述的雷达放置区间中都有点存在(从而每个岛屿都有雷达覆盖)。

   则使用贪心算法即可解决问题(此即刘汝佳白书P153:区间选点问题)。


=======================================代码=======================================




#include<cmath>#include<cstdio>#include<algorithm>using namespace std;int N,R;struct Interval { double sta,end; } Inter[1005];bool cmp(Interval& I1,Interval& I2)  {return (I1.end<I2.end)||(I1.end==I2.end&&I1.sta>I2.sta);}bool ReadAndDealData()                  //读取并处理数据{bool flag=true;                     //数据合理标志for(int i=0;i<N;++i){double x,y;scanf("%lf%lf",&x,&y);if(flag==true)                  //只有在未发现不合理数据时才继续处理{if(R<y)                     //存在不合理数据{ flag=false; }else                        //计算可以覆盖第i个岛屿的雷达放置区间{Inter[i].sta=x-sqrt(R*R-y*y);Inter[i].end=2*x-Inter[i].sta;}}}return flag;}int main(){for(int cas=1;scanf("%d%d",&N,&R)==2&&(N||R);++cas){int cnt=-1;if(ReadAndDealData()==true)     //只有在未发现不合理数据时才继续处理{sort(Inter,Inter+N,cmp);cnt=1;double coverend=Inter[0].end;for(int i=1;i<N;++i)        //贪心算法{if(coverend<Inter[i].sta){++cnt; coverend=Inter[i].end;}}}printf("Case %d: %d\n",cas,cnt);}return 0;}

原创粉丝点击