贪心算法之区间取点问题

题目描述:
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.

输入
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

输出
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.
样例输入
3 2
1 2
-3 1
2 11 2
0 20 0
样例输出
Case 1: 2
Case 2: 1

解题思路:

此题意思给定点集S={(xi,yi)i=1.2.3...n},求用圆心在x轴上,半径为d的圆覆盖S所需的圆的最少个数。

1.先把给出的岛的坐标(xi.yi)和半径r转化为在x轴上的区间,即当d-yi>=0时,圆心位于x轴上的区间为Ii=[ xi-sqrt(d^2-yi^2) , xi + sqrt( d^2 - yi^2 )],则转化为区间选点问题。

2.S中点(xi,yi),对应一个在x轴上的区间Ii=(li,ri),按照区间右端点ri从小到大排序,在区间集合中选择一个索引最小的区间,把选择的区间和与其相交的所有区间作为一组从T中删除,直到T为空集、

3则剩下的分组的组数即为m的最小值。。。

AC代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
#define MAX 1010using namespace std;class Range
{public:float left;float right;bool operator<(const Range &r)const{if(right<r.right||(right==r.right&&left>r.left))return true;elsereturn false;}
};int main(int argc,char *argv[])
{int n,d;int x,y;int i,j;int count=1,ans;while(scanf("%d%d",&n,&d)&&(n+d)){Range r[MAX];j=0;ans=0;for(i=0;i<n;i++){Range temp;scanf("%d%d",&x,&y);if(d>=y){temp.left=x-sqrt(d*d-y*y);temp.right=x+sqrt(d*d-y*y);r[j++]=temp;}}sort(r,r+j);if(j<n)printf("Case %d: -1\n",count);else{float end=-0x0FFFFFFF;for(i=0;i<j;i++){if(end<r[i].left){ans++;end=r[i].right;}}printf("Case %d: %d\n",count,ans);}count++;}return 0;
}

解题方法跟求最大区间数极其相似。。。

贪心算法之区间取点问题相关推荐

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

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

  2. 贪心算法:区间调度-射气球问题

    贪心算法:区间调度问题 母问题描述: 在一个会场中,安排活动,给定所有活动的开始时间与结束时间的集合放在数组nums[n][2]中.求问最多能安排几场活动,使每场活动之间的时间不冲突. public ...

  3. 贪心算法【区间调度】【背包问题】【集合覆盖】【旅行商问题】【哈夫曼构造价值树】

    贪心算法  在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解.  贪心算法不是对所有问题都能得到整体最优解,关键是贪心策略的选择, ...

  4. 贪心算法——合并区间(Leetcode 56)

    题目选自Leetcode56 思路 大家应该都感觉到了,此题一定要排序,那么按照左边界排序,还是右边界排序呢? 都可以!排序之后就是贪心思想了~~ 题目 给出一个区间的集合,请合并所有重叠的区间. 示 ...

  5. NYOJ 6 喷水装置(一) 贪心算法 之 区间覆盖问题

    喷水装置(一) 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷水装置,每个喷水装置的效果都会让以它为 ...

  6. 算法基础复盘笔记Day12【贪心算法】—— 区间问题、Huffman树、排序不等式、绝对值不等式、推公式

    ❤ 作者主页:欢迎来到我的技术博客

  7. [Leedcode][JAVA][第56题][合并区间][数组][贪心算法]

    [问题描述]56.合并区间 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18] ...

  8. 基于贪心算法的几类区间覆盖问题 nyoj 12喷水装置(二) nyoj 14会场安排问题...

    1)区间完全覆盖问题 问题描述:给定一个长度为m的区间,再给出n条线段的起点和终点(注意这里是闭区间),求最少使用多少条线段可以将整个区间完全覆盖 样例: 区间长度8,可选的覆盖线段[2,6],[1, ...

  9. 贪心算法——区间选点问题

    转载:https://blog.csdn.net/xia842655187/article/details/51944763 区间选点的问题大致可以描述为:  给定N个区间[a,b],取尽量少的点,使 ...

最新文章

  1. “ yield”关键字有什么作用?
  2. 钩子运行机制HOOK
  3. 第一章 代码无错就是优吗?(简单工厂模式)
  4. java反射机制深入详解_Java基础与提高干货系列——Java反射机制
  5. 华为系统里的计算机,一个屏幕操作两个系统 让你的手机装进华为MateBook 14电脑里...
  6. C++矩阵加速经典题目:Warcraft III 守望者的烦恼 [vijos 1067]
  7. 关于编码问题,报错:'gbk' codec can't encode character '\u3164' in position 0: illegal multibyte sequence...
  8. 用户体验报告(Echo)
  9. Damp;G“辱华”争议广告女主发声:几乎断送了模特事业
  10. 2021-09-07冒泡排序
  11. HTML5页面实现文件下载
  12. 蓝桥杯真题 Python A组 路径
  13. logistic回归分析优点_logistic回归模型分析
  14. ABP .Net Core 日志组件集成使用NLog
  15. InnoDB中的数据库索引
  16. svnserve: 不能绑定服务器套接字: 地址已在使用
  17. C语言实现来实现字符串反转,只有单词顺序反转,组成单词的字母不反转
  18. 树莓派笔记01—35美元的Linux超级计算机
  19. Python黑科技:暴力破解你的密码
  20. NLP-生成模型-2017-PGNet:Seq2Seq+Attention+Coverage+Copy【Coverage解决解码端重复解码问题;Copy机制解决解码端OOV问题】【抽取式+生成式】

热门文章

  1. MongoDB学习笔记(三)-----集群架构
  2. word2vec学习+使用介绍
  3. html 代码 border,HTML Style border用法及代码示例
  4. 国培-甘肃定西市新任职校长培训班莅临湖南省智慧教育装备展示体验中心参观学习
  5. FreeMarker模板语言开发(整理版)
  6. 一款优秀的智慧社区系统应具备哪些功能
  7. 使用java爬取数据的三种思路
  8. Java字符串模板格式化汇总8法(附性能对比)
  9. c语言产生随机数停滞,C语言产生随机数,个人理解
  10. 7大赚钱思维,句句灼心!