【贪心算法】POJ-1328 区间问题
一、题目
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;
}
【贪心算法】POJ-1328 区间问题的更多相关文章
- 【贪心算法】POJ-2376 区间问题
一.题目 Description Farmer John is assigning some of his N (1 <= N <= 25,000) cows to do some cle ...
- Radar Installation(POJ 1328 区间贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 68578 Accepted: 15 ...
- 【贪心算法】POJ-3190 区间问题
一.题目 Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one wil ...
- 贪心 POJ 1328 Radar Installation
题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...
- 基于贪心算法的几类区间覆盖问题 nyoj 12喷水装置(二) nyoj 14会场安排问题
1)区间完全覆盖问题 问题描述:给定一个长度为m的区间,再给出n条线段的起点和终点(注意这里是闭区间),求最少使用多少条线段可以将整个区间完全覆盖 样例: 区间长度8,可选的覆盖线段[2,6],[1, ...
- 贪心算法----区间选点问题(POJ1201)
题目: 题目的大致意思是,给定n个闭区间,并且这个闭区间上的点都是整数,现在要求你使用最少的点来覆盖这些区间并且每个区间的覆盖的点的数量满足输入的要求点覆盖区间的数量. 输入: 第一行输入n,代表n个 ...
- POJ 1328 Radar Installation 贪心 A
POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...
- POJ 1328 Radar Installation【贪心】
POJ 1328 题意: 将一条海岸线看成X轴,X轴上面是大海,海上有若干岛屿,给出雷达的覆盖半径和岛屿的位置,要求在海岸线上建雷达,在雷达能够覆盖全部岛屿情况下,求雷达的最少使用量. 分析: 贪心法 ...
- poj 1088 滑雪(贪心算法)
思想: (贪心算法 ,看到题目是中文才做的) 先对数组中的数据进行排序,从最小的数据计算 当前的顶点的可以滑行的最大值=max(周围可达的顶点的可以滑行的最大值)+1 这样计算最后产生的路径肯定是最大 ...
随机推荐
- 20145234黄斐《Java程序设计》实验五—网络安全与编程
1: 两人一组结对编程: 0. 参考http://www.cnblogs.com/rocedu/p/6766748.html#SECDSA 1. 结对实现中缀表达式转后缀表达式的功能 MyBC.jav ...
- .net core的定时任务框架Timed Job
参考文档:http://www.1234.sh/post/pomelo-extensions-timed-jobs 在该文档中介绍了怎么使用timed job,但是在使用db的时候会发生错误,错误一般 ...
- REST API 开发
本文我们将使用Spring MVC 4实现 CRUD Restful WebService , 通过RestTemplate写一个 REST 客户端,定义这些服务. 我们也可以通过外部的一些客户端来测 ...
- mysql索引原理及查询速度优化
一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...
- 体验搜狐PaaS平台搜狐云景-自动调度(Autoscale)
今天,收到一封「搜狐云景」送邀请码的邮件,价值 200 rmb,立马前往官网简单了解一下,这个玩意儿是搜狐公司云战略的一个产品,一个 PaaS 平台,简单了解了一下特性: 1.自由定制运行环境,这表示 ...
- 2018年美国大学生数学建模竞赛(MCM/ICM) E题解题思路
任务一就是让大家去做个基本的评价,是典型的评价类问题,所以应该按照 指标+方法的步骤去做,首先就是寻找国家脆弱性的相关概念,然后选择影响国 家脆弱性的指标,如气候变化,经济发展,政治状况等等,再就是构 ...
- bintray 在android3.2上传遇到的问题
1.报错信息如下: Gradle DSL method not found: 'google()'Possible causes: The project 'JustTest' may be usin ...
- 流行创意风格教师求职简历免费word模板
18款流行创意风格教师求职简历免费word模板,也可用于其他专业和职业,个人免费简历模板,个人简历表免费,个人简历表格. 声明:该简历模板仅用于个人欣赏使用,请勿用于商业用途,谢谢. 下载地址:百度网 ...
- Rxjava - 操作符,线程操作的简单使用
目录 创建操作符 10种常用的操作符定义 下面做几个操作符的demo演示 create from repeat defer interval Scheduler 什么是Scheduler? 如何使用S ...
- linux分区满了,如何进行扩容
转自:https://blog.csdn.net/valage/article/details/73332147 图片中可以看到挂载点“/”的利用率移到100%,空间不够,所以要对其进行分区. 1. ...