一、题目

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

1 2
0 2

0 0

Sample Output

Case 1: 2
Case 2: 1

二、思路&心得

  • 贪心算法与区间问题:以每个岛屿的坐标为中心,d为半径构造圆,该圆与X轴的两个交点即构成一个区间,若在这个区间上的任何雷达均可扫描到该岛屿。通过对n个岛屿进行处理,可得到n个区间,则问题转化成区间问题。
  • 每个区间a[i]有两个端点:first和second,对区间数组a按second进行升序排序,然后从左向右扫描,对于每一个区间a[i],若a[i].first小于之前选择的second的值,则不做任何处理,知道找到大于second的区间,然后进行下一个循环。
  • 在数据输入的时候可进行特判,如若有岛屿的Y坐标大于d或则Y坐标<0或则d<0,则输入完成后直接返回-1即可。
  • 数据定义记得用浮点型进行定义。
  • PS:在做贪心问题时,务必确定所做的贪心选择的正确性,在做这题时因为一开始的方向就是错误的,导致浪费了很多时间。

三、代码

#include<cstdio>
#include<cmath>
#include<algorithm>
#define MAX_SIZE 1005
using namespace std;typedef pair<double, double> P;P a[MAX_SIZE];int n, ans;double x, y, d;bool cmp(P a, P b) {if (a.second < b.second) return true;else return false;
}int solve() {bool flag = true;double end;ans = 0;for (int i = 0; i < n; i ++) {scanf("%lf %lf", &x, &y);if (y > d || y < 0) flag = false;if (flag) {a[i].first = x - sqrt(d * d - y * y);a[i].second = x + sqrt(d * d - y * y);}}if (!flag || d < 0) return -1;sort(a, a + n, cmp);for (int i = 0; i < n; i ++) {if (flag) {end = a[i].second;ans ++;flag = false;continue;       }if (a[i].first > end) {flag = true;i --;}}return ans;
}int main() {int step = 1;while (~scanf("%d %lf", &n, &d)) {if (!n && !d) break;printf("Case %d: %d\n", step ++, solve());getchar();}return 0;
}

转载于:https://www.cnblogs.com/CSLaker/p/7285114.html

【贪心算法】POJ-1328 区间问题相关推荐

  1. 贪心算法无重叠区间c语言,贪心算法之区间问题.md

    --- title: 贪心算法之区间问题 tags: - Leetcode categories: - Leetcode author: 四叶草 top: false abbrlink: 26230 ...

  2. java区间合并_贪心算法:合并区间

    ❝ 最近文章阅读量少了很多啊打卡也少了, 是不是年底了很多录友在忙期末考试啊,哈哈. 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: intervals = [[1,3],[2,6], ...

  3. 【贪心算法】POJ-2376 区间问题

    一.题目 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cle ...

  4. 贪心算法——选择不相交区间问题

    题目描述:设有n个活动的集合,其中每个活动都要求使用同一个资源,而在同一时间内只有一个活动能够使用这一资源,每个活动i都有一个要求使用该资源的起始时间si和一个结束时间fi(si<fi),如果选 ...

  5. 贪心算法-----poj 3253 Fence Repair(切木板)

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  6. leetcode 贪心_leetcode题解(贪心算法)

    定义 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择. 也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解. 通常贪心算法的代码会非常短而且思路也非常 ...

  7. 4.贪心算法 含例题

    文章目录 贪心算法 一.一个基本的贪心算法问题:区间调度问题 二.区间调度的推广:多个资源下的贪心算法 三.最小延迟调度--交换论证 四.最优超高速缓存问题 五.图最短路径问题 六.最小生成树问题 七 ...

  8. _32LeetCode代码随想录算法训练营第三十二天-贪心算法 | 738.单调递增的数字 、714.买卖股票的最佳时机含手续费、968.监控二叉树

    _32LeetCode代码随想录算法训练营第三十二天-贪心算法 | 738.单调递增的数字 .714.买卖股票的最佳时机含手续费.968.监控二叉树 题目列表 738.单调递增的数字 714.买卖股票 ...

  9. 贪心算法—建立雷达(POJ 1328)

    贪心算法--区间选点问题 这也是贪心算法的经典问题,一般情况为:有n个闭区间[ai,bj],取尽量少的点,使得每个区间内都至少有一个点. 分析 如果区间i内已经有一个点被取到,则称此区间已经被满足. ...

  10. 贪心算法—区间调度 电影节(POJ 4151)

    贪心算法--区间选取问题 或是区间调度问题 本文解决一个很经典的贪心算法问题 Interval Scheduling(区间调度问题).给你很多形如[start,end]的闭区间,请你设计一个算法,算出 ...

最新文章

  1. O/R Mapping 研究报告(转)
  2. php自定义扩展函数,Laravel框架中扩展函数、扩展自定义类的方法
  3. PHP项目部署在tomcat,在Tomcat中部署Web项目的操作方法(必看篇)
  4. iphone震动反馈怎么设置_如何评价 iPhone 上的振动反馈?
  5. Maven的单元测试插件maven-surefire-plugin详解
  6. 设计几个简单的汇编函数
  7. matlab 超限像素平滑法,matlab超限像素平滑法_图像增强技术.ppt
  8. SpringBoot(12)---外部化配置(properties文件配置)
  9. java实现订单物品计算佣金,java三角形、NextDay、佣金问题代码
  10. http://www.jianshu.com/p/42e11515c10f#
  11. html出现滚动条页面闪动,CSS3 calc实现滚动条出现页面不跳动闪动
  12. Sharepoint SSL(Https)
  13. ssh配置config文件
  14. 实现局部滚动的两种方法:1.三行css代码2.使用BScroll框架
  15. jquery实现多选框
  16. 120日均线金叉250日均线是大牛市来临的重要信号
  17. 董桥《南山雨》(选自:旧时月色)
  18. 迅视财经 五条特色大街上线
  19. ps考试引擎安装溢出屏幕
  20. 仙剑5手游服务器维护,《仙剑奇侠传》手游维护内容官方解答

热门文章

  1. 湖畔第一大脑蒋烁淼,爱技术也爱创业,送给正在奋斗的你
  2. python时间和周期_python实现以立春为起点n为周期任意日期所在的日期区间
  3. 全国大学生大数据技能竞赛比赛心得以及相关资料
  4. 鸿蒙 background_element设置渐变色
  5. 全屏动态滑稽网站HTML源码
  6. 声势浩大发展云服务的金蝶,如今“破茧”了吗
  7. wp8.1 java_巨硬的内部比较——WP8.1版本与WP10系统对比(以lumia640为例)
  8. unity3d显示c4d材质_纯干货:C4D从初学者到精通,其实很简单
  9. 优动漫PAINT实现制图化繁为简的一波骚操作
  10. 如何升级Microsoft Edge浏览器