【贪心算法】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 这样计算最后产生的路径肯定是最大 ...
随机推荐
- PHP通过_call实现多继承
原文地址:http://small.aiweimeng.top/index.php/archives/53.html 上一篇讲到php可以通过接口是实现代码的复用. 那么这篇文章简单介绍下使用_cal ...
- R语言学习笔记(二十一):字符串处理中的元字符(代码展示)
元字符有自己的特殊含义 [ ]内的任意字符将被匹配 grep(pattern = "[wW]", x = states, value = T) grep(pattern = &qu ...
- 使用源安装java JDK
使用下面的命令安装,只需一些时间,它就会下载许多的文件,所及你要确保你的网络环境良好: sudo add-apt-repository ppa:webupd8team/java sudo apt-ge ...
- 5 功能4:个人站点页面设计(ORM跨表与分组查询)
1.个人站点页面文章的查询 # 个人站点 http://www.cnblogs.com/wh1520577322/ http://www.cnblogs.com/liucong12345/ http: ...
- 洛谷 P4478 [BJWC2018]上学路线
洛谷 P4478 [BJWC2018]上学路线 原题 神仙题orz,竟然没有1A....容斥+卢卡斯+crt?? 首先用容斥做,记\(f[i][0/1]\)表示到i号点经过了奇数/偶数个点的方案数,因 ...
- P2939 [USACO09FEB]改造路Revamping Trails
P2939 [USACO09FEB]改造路Revamping Trails 同bzoj2763.不过dbzoj太慢了,bzoj又交不了. 裸的分层图最短路. f[i][j]表示免费走了j条路到达i的最 ...
- 牛客OI周赛3-提高组-B-1408[dp]
题意 给你长度为 \(n\) 的两个排列 \(A,B\) 组成的序列,求最少的交换相邻数字的次数使得 \(A,B\) 分别有序. \(n\leq 2000\) . 分析 如果只有一个排列时最少交换次数 ...
- 在Javascript中 声明时用"var"与不用"var"的区别,== 和 ===的区别
今天,被问到两个JS问题,当时没回答到重点,问题虽然看起来简单,但是细节却马虎不得,在此做下记录: 1. 在Javascript中 声明时用"var"与不用"var&qu ...
- C# string 的一点属性、方法什么的
今天学的基本可以说是都属于方法和属性 下面这两句话非常重要,确实非常重要 凡是可以 “ . ” 出来的,前面是黑色的小扳手的:属性 紫色的立方体的:方法 这个对于以后自学帮助是不小的,当然, ...
- 干货分享:五大最适合学习AI开发的编程语言
AI(人工智能)为应用开发者开创了一个全新的可能性.通过利用机器学习或深度学习,您可以生成更好的用户配置文件.个性化设置和推荐,或者整合更智能的搜索.语音界面或智能助手,或者以其他数种方式改进您的应用 ...