一、题目

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. WP10的一点小问题

    兼容WP8.0/WP7.5不太完整!也许是测试版的问题.毕竟还没发布正式版! 具体如:WP8.0或WP7.5的启动器!就是选择图片的启动器!调用后对话框返回的结果都是Cancel本来应该是OK的.也就 ...

  2. 1 nodejs

      重点 :  

  3. 在Javascript中 声明时用"var"与不用"var"的区别,== 和 ===的区别

    今天,被问到两个JS问题,当时没回答到重点,问题虽然看起来简单,但是细节却马虎不得,在此做下记录: 1. 在Javascript中 声明时用"var"与不用"var&qu ...

  4. python并发编程之多进程理论知识

    一 什么是进程 进程:正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 举例(单核+多道,实现多个进程的并发执行): egon在一个时间段内有很多任务要做:python备课的任务,写书的任 ...

  5. Android Studio: Could not download junit.jar (junit:junit:4.12)

    下载了Android Studio 3.1.4,新建一个项目,选择Activity,结果gradle提示 Unable to resolve dependency for ':app@debugAnd ...

  6. 用Micro:bit播放生日快乐歌

    Micro:bit自带一个有趣的功能就是可以生成音乐播放,今天做一个简单实用的案例,用Micro:bit播放生日快乐歌. 算法: 按下按键A,显示生日快乐 播放D 播放D 播放E 播放D 播放G 播放 ...

  7. 树莓派UPS-18650,添加时钟

    1.简介 UPS-18650 是一个专门为树莓派(以下简称 pi)所设计的 UPS 电源,采用两颗标准 的 18650 锂电池进行供电,支持外部电源插入检测,支持边充边放,既插上外部电源时, pi 由 ...

  8. springboot+elasticsearch实现全局检索

    ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.Elasticsearch是用Java开发的,并作为Apach ...

  9. NAT概念解释(不完全版,但不会搞错...)

    NAT在计算器网络中,网络地址转换(Network Address Translation,缩写为NAT),也叫做网络掩蔽或者IP掩蔽(IP masquerading)是一种IP数据包在通过路由器或防 ...

  10. pytorch 如何使用tensorboard实时查看曲线---- tensorboardX简介

    用惯了tensorflow的小伙伴肯定都用过tensorboard工具吧.虽然Facebook也推出了visdom,但是在一次不小心误触clear之后,我放弃了这个工具(页面的一个clear按钮我本来 ...