ACM POJ 1328 Radar Installation

Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu

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.

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 2
1 2
-3 1
2 1

1 2
0 2

0 0


Sample Output

Case 1: 2

Case 2: 1



题目大意: x轴上方是海,下面是陆地,海面上给出若干个小岛,现在给出灯塔可见半径d和每个灯塔的坐标,要求在陆地上选择建最少的灯塔保证每个岛都能可见,无解输出-1;


分析:首先如果无解,那么一定存在某个灯塔的坐标y大于灯塔半径k,直接输出-1;

如果有解,那么每个灯塔在海岸线上一定存在一个灯塔的可建范围l,r;

此时我们只需要从这些区间当中找到最少灯塔即可。


那么问题来了,如何找呢?

|

|

|

贪心!


我们将每个小岛按照右范围r从小到大排好,从最左边的岛开始,只要接下来的岛的左范围l小于当前岛的r,那么这两个岛一定可以共用一个灯塔,一直到范围r外的小岛,那么此时肯定需要建立其他灯塔才能满足解。


代码如下:


<script src="https://code.csdn.net/snippets/741681.js"></script>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ACM Codebook(ACM代码手册)是一本整理了算法竞赛常用算法和数据结构的参考手册。它是为了帮助算法竞赛选手快速查阅各种算法和数据结构的实现而编写的。 ACM Codebook的内容非常丰富,包括了各种常用算法,如排序算法、图论算法、动态规划算法等。同时,它还包含了各种常用数据结构的实现,如链表、栈、队列、堆等。此外,ACM Codebook还介绍了一些常见的算法设计技巧和优化技巧,帮助选手更好地解决问题。 ACM Codebook的作用非常明显,首先它提供了各种算法和数据结构的实现代码,方便选手直接复制粘贴使用,节省了编写代码的时间。其次,ACM Codebook提供了详细的算法和数据结构的说明和示例,帮助选手理解和掌握这些算法和数据结构的原理和用法。最后,ACM Codebook还提供了一些常见问题的解决方案,帮助选手快速解决问题。 ACM Codebook的编写并不容易,需要作者具备扎实的算法和数据结构基础,并且对算法竞赛有深入的了解。编写ACM Codebook需要不断地修改和更新,以适应算法竞赛中不断变化的需求。 总之,ACM Codebook是一本非常有用的参考手册,它不仅提供了丰富的算法和数据结构的实现,还提供了对应的说明和示例,帮助算法竞赛选手快速掌握和应用这些算法和数据结构。它是算法竞赛选手在比赛中必备的工具之一。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值