博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
△POJ1328--Radar Installation(贪心)
阅读量:4950 次
发布时间:2019-06-11

本文共 2830 字,大约阅读时间需要 9 分钟。

Radar Installation

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

题目大意(转):是为了求出能够覆盖所有岛屿的最小雷达数目,每个小岛对应x轴上的一个区间,在这个区间内的任何一个点放置雷达(在x轴上),则可以覆盖该小岛,区间范围的计算用[x-sqrt(d*d-y*y),x+sqrt(d*d-y*y)]。

这样,问题即转化为已知一定数量的区间,求最小数量的点,使得每个区间内斗至少存在一个点。每次迭代对于第一个区间, 选择最右边一个点, 因为它可以让较多区间得到满足,。如果不选择第一个区间最右一个点(而选择前面的点), 那么把它换成(前面区间)最右的点之后, 以前得到满足的区间, 现在仍然得到满足。

 

1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 7 #define maxn 1001 8 9 struct node10 {11 double left;12 double right;13 }island[maxn];14 15 bool cmp(node a,node b)16 {17 return a.left
d||d<0)33 {34 flag=1;35 }36 island[i].left=x-sqrt(d*d-y*y);//先算出每个岛的雷达覆盖区间37 island[i].right=x+sqrt(d*d-y*y);//先算出每个岛的雷达覆盖区间38 }39 if(flag)40 {41 printf("Case %d: -1\n",k++);42 continue;43 }44 sort(island,island+n,cmp);//按左端点排序45 temp=island[0].right;//排序完成后,第一个雷达建立在第一个区间的右端点46 count=1;47 for(i=1;i
temp)//判断每个区间的左端点,如果在最新建立的雷达右边54 {55 count++;//那么肯定需要一个新雷达56 temp=island[i].right;//而且也建在该区间右端57 }58 }59 printf("Case %d: %d\n",k++,count);60 }61 return 0;62 }

 

转载于:https://www.cnblogs.com/youdiankun/p/3707357.html

你可能感兴趣的文章
WPF自定义搜索框代码分享
查看>>
js 基础拓展
查看>>
Windows下常用测试命令
查看>>
SpringBoot访问html访问不了的问题
查看>>
{width=200px;height=300px;overflow:hidden}
查看>>
SDK提交到CocoaPods
查看>>
C#生成随机数
查看>>
Flask-jinja2
查看>>
CSS基础学习 20.CSS媒体查询
查看>>
JavaScript全面学习(node.js)
查看>>
I/O模式总结
查看>>
2019春季第十一周作业
查看>>
洛谷P4591 [TJOI2018]碱基序列 【KMP + dp】
查看>>
iOS CoreData介绍和使用(以及一些注意事项)
查看>>
OS笔记047代理传值和block传值
查看>>
Android应用程序与SurfaceFlinger服务的连接过程分析
查看>>
coco2dx服务器简单例子
查看>>
Java回顾之多线程
查看>>
sqlite
查看>>
机电行业如何进行信息化建设
查看>>