贪心算法解题报告(区间覆盖问题)
题目:
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距离,所以如果小岛能够被覆盖到的话,它们之间的距离最多为d。题目要求计算出能够覆盖给出的所有岛屿的最少雷达数目。
思路:对于每个小岛,我们可以将每一个雷达所在位置投影到x轴上。问题转化为如何用尽可能少的点覆盖这些区间。先将所有区间按照左端点大小排序。若两区间重合只需要放两个,若两区间互不重合,则需要两个。如果一个区间完全包含于另外一个区间,我们需要更新区间的右端点;如果两个区间不相交,我们需要增加点并更新右端点。
代码:
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
struct Point
{double x;double y;
}m[1000];
int cmp(const void *a, const void *b)
{return (*(Point *)a).x>(*(Point *)b).x?1:-1;
}
int main()
{int n,d;int num=1;while(cin>>n>>d){int k=1;if(n==0&&d==0) break;for(int i=0;i<n;i++){int x,y;cin>>x>>y;if(y>d){k=-1;}double t=sqrt(d*d-y*y);m[i].x=x-t;m[i].y=x+t;}if(k!=-1){qsort(m,n,sizeof(m[0]),cmp);double s=m[0].y;for(int i=1;i<n;i++){if(m[i].x>s){k++;s=m[i].y;}else if(m[i].y<s){s=m[i].y;}}}cout<<"Case "<<num<<':'<<' '<<k<<endl;num++;}
}
贪心算法解题报告(区间覆盖问题)相关推荐
- 贪心算法解题报告(D-Farmer John)
题目: Farmer John has a problem: the dirt road from his farm to town has suffered in the recent rainst ...
- DAY1 贪心算法学习报告
集训DAY1:贪心算法 学习报告 这天的题还有一道未解决,暂时不会代码实现,由于时间有限(精力是相对无限的),所以留待明天补档. /(课堂笔记) 贪心算法的核心:局部最优得整体最优 证明:数学归纳 微 ...
- java区间合并_贪心算法:合并区间
❝ 最近文章阅读量少了很多啊打卡也少了, 是不是年底了很多录友在忙期末考试啊,哈哈. 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: intervals = [[1,3],[2,6], ...
- matlab求解集合覆盖问题,贪心算法实践之集合覆盖问题
介绍 贪婪算法(贪心算法)是指在对问题进行求解时,在每一步选择中都采取最好或者最优(即最有利)的选择,从而希望能够导致结果是最好或者最优的算法 贪婪算法所得到的结果不一定是最优的结果(有时候会是最优解 ...
- 贪心算法讲解(集合覆盖问题,旅行商问题求解)
教室调度问题 假设有如下课程表,你希望将尽可能多的课程安排在某间教室上. 你没法让这些课都在这间教室上,因为有些课的上课时间有冲突. 你希望在这间教室上尽可能多的课.如何选出尽可能多且时间不冲突的课程 ...
- 贪心算法无重叠区间c语言,贪心算法之区间问题.md
--- title: 贪心算法之区间问题 tags: - Leetcode categories: - Leetcode author: 四叶草 top: false abbrlink: 26230 ...
- 【贪心算法】POJ-2376 区间问题
一.题目 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cle ...
- 贪心算法——选择不相交区间问题
题目描述:设有n个活动的集合,其中每个活动都要求使用同一个资源,而在同一时间内只有一个活动能够使用这一资源,每个活动i都有一个要求使用该资源的起始时间si和一个结束时间fi(si<fi),如果选 ...
- 区间覆盖全部类型及部分精选习题汇总详解(贪心策略)
内容如下: 1)区间完全覆盖问题 问题描述:给定一个长度为m的区间,再给出n条线段的起点和终点(注意这里是闭区间),求最少使用多少条线段可以将整个区间完全覆盖 样例: 区间长度8,可选的覆盖线段[2, ...
最新文章
- 浙大版《C语言程序设计(第3版)》题目集 练习2-17 生成3的乘方表 (15 分)
- R语言使用ggplot2包使用geom_boxplot函数绘制基础分组水平箱图(boxplot)实战
- tc c语言弹出式下拉式菜单,c语言制作弹出式菜单
- 接口作为参数,不同的接口调用不同的方法,例如:输出“I love Game”或输出“我喜欢游戏”...
- Hi3516A开发--编译整个osdrv目录所遇到的问题总结
- Q:一个经典的helloworld程序需要几个文件?
- python课堂整理9---函数1
- 中职一年级计算机英语课件,中职生一年级英语.doc
- php json 特殊字符,PHP转义Json里的特殊字符的函数
- python文件拷贝到其他盘符_Python:文件操作技巧(File operation)
- Linux下MariaDB 安装及root密码设置(修改)
- 使用R包GD实现地理探测器算法
- python运维自动化老男孩_Day1 老男孩python自动化运维课程学习笔记
- JQuery监听页面滚动总结
- linux 读取内存颗粒,Linux中的内存管理模型浅析
- Ratione aspernatur nam dolorem vitae quia.Fumer comme créer passer ailleurs jouer lumière.
- Packet Data Convergence Protocol (PDCP)阅读笔记
- linux下opengl开发环境,Linux下配置OpenGL开发环境
- 直播APP开发技术原理分享
- Redis项目应用场景与实例(三):队列(List)
热门文章
- [推荐]应当承认‘外来人口’的租户权
- 浏览计算机已查找驱动程序软件,魅族Pro6plus线刷救砖ROM包_内附教程可救砖
- B站小学生们当起了算法老师,这么卷么?
- vue.js 小数点保留两位 和 一位小数
- java fel_FEL表达式的用法
- JAVA FEL表达式的使用
- python代码大全心形盒子简单_一行Python代码画心型
- np.diff二维数组中使用append和prepend,
- 基于单片机的倒车雷达源程序及AD仿真图
- nginx重启后配置文件不生效问题