一、题目

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 区间问题的更多相关文章

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

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

  2. Radar Installation(POJ 1328 区间贪心)

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 68578   Accepted: 15 ...

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

    一.题目 Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one wil ...

  4. 贪心 POJ 1328 Radar Installation

    题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...

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

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

  6. 贪心算法----区间选点问题(POJ1201)

    题目: 题目的大致意思是,给定n个闭区间,并且这个闭区间上的点都是整数,现在要求你使用最少的点来覆盖这些区间并且每个区间的覆盖的点的数量满足输入的要求点覆盖区间的数量. 输入: 第一行输入n,代表n个 ...

  7. POJ 1328 Radar Installation 贪心 A

    POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...

  8. POJ 1328 Radar Installation【贪心】

    POJ 1328 题意: 将一条海岸线看成X轴,X轴上面是大海,海上有若干岛屿,给出雷达的覆盖半径和岛屿的位置,要求在海岸线上建雷达,在雷达能够覆盖全部岛屿情况下,求雷达的最少使用量. 分析: 贪心法 ...

  9. poj 1088 滑雪(贪心算法)

    思想: (贪心算法 ,看到题目是中文才做的) 先对数组中的数据进行排序,从最小的数据计算 当前的顶点的可以滑行的最大值=max(周围可达的顶点的可以滑行的最大值)+1 这样计算最后产生的路径肯定是最大 ...

随机推荐

  1. AngularJS中的按需加载ocLazyLoad插件应用;

    一.前言 ocLoayLoad是AngularJS的模块按需加载器.一般在小型项目里,首次加载页面就下载好所有的资源没有什么大问题.但是当我们的网站渐渐庞大起来,这样子的加载策略让网速初始化速度变得越 ...

  2. spark 基础开发 Tips总结

    本篇博客主要是 sparksql 从初始开发注意的一些基本点以及力所能及的可优化部分的介绍: 所使用spark版本:2.0.0       scala版本:2.11.8 1. SparkSession ...

  3. 树莓派3B+学习笔记:1、安装官方系统

    1.登录树莓派官方网站www.raspberrypi.org,点击Downloads: 2.点击NOOBS: 3.选择下载方式,可以选择下载BT种子或直接下载,这里我用迅雷直接下载,下载速度还是很快的 ...

  4. exBSGS学习笔记

    exBSGS学习笔记 Tags:数学 题目的话就做下洛谷的模板好了 // luogu-judger-enable-o2 #include<algorithm> #include<io ...

  5. CentOS7 msmtp+mutt发送邮件

    一.安装软件 # msmtp软件各版本下载地址:https://marlam.de/msmtp/download/ [root@--- ~]# wget https://marlam.de/msmtp ...

  6. opencv-Getting Started with Videos

    1.opencv库简单操作视频 # coding = utf-8 # Getting Started with Videos import cv2 import numpy as np # 创建捕获视 ...

  7. JAVAWEB tomcat服务器启动错误原因总结

    tomcat服务器启动错误: org.apache.catalina.LifecycleException    这种异常的原因是  servlet的代码出现了错误 实例: 这里的servlet由于使 ...

  8. Windows:使用Dos命令管理服务(Services)

    Windows 服务器系列: Windows:查看IP地址,IP地址对应的机器名,占用的端口,以及占用该端口的应用程 Windows:使用Dos命令管理服务(Services) Windows:任务调 ...

  9. SpringBoot日记——按钮的高亮和添加篇

    场景如下: 我们点击主页,主页那个按钮就高亮: 我们点击员工,员工那个按钮就高亮: 高亮的处理 直接来看代码如何编写: 1.先看一下官方文档如何编写关于参数配置的,等下我们来解释为何这么写: 所以,我 ...

  10. SpringBoot日记——实战篇——Url定向

    搞定了SpringBoot的一些基础核心的东西,我们需要实践一个项目来确认自己学习的东西能被应用,最初,我们会选择自己写一个登陆页面,这也是每个网站几乎都有的门面. 在写之前,还有一些知识点需要记录— ...