【贪心算法】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 这样计算最后产生的路径肯定是最大 ...
随机推荐
- 一、用Delphi10.3 创建一条JSON数据
一.用Delphi10.3构造一个JSON数据,非常之容易,代码如下: uses System.JSON; procedure TForm1.Button1Click(Sender: TObject) ...
- 让你的:active好好工作
在某些版本的safari上,:active伪类不生效,可以通过css和js两种方式hack一下: html <div class="button"> 点击时我应该变红 ...
- springboot快速入门(五)——事务管理
一.入门 概念就不再赘述了,由于一般我们是通过service控制事务,这里给出注解式的示例: package com.example.demo; import com.example.demo.bea ...
- sqlplus 格式化一例
对字符型,用axx格式,对数字型,用9999 格式(999表示占用3列) SQL> col 'ts#' format 999SQL> col 'file#' format 999SQL&g ...
- 18 [网络编程]-UDP
1.TCP VS UDP tcp基于链接通信 基于链接,则需要listen(backlog),指定连接池的大小 基于链接,必须先运行的服务端,然后客户端发起链接请求 对于mac系统:如果一端断开了链接 ...
- <link rel="stylesheet" type="text/css" href="css/index.css">详解
整条语句的含义是: 调用一个外部的CSS样式文件.他是通过<link/>这个标签来调用的. 然后, href="css/index.css" 表示外部样式文件的路径, ...
- Object C学习笔记4-内存管理
Object-C的内存管理和.NET有些不一样,.NET的内存回收机制是使用GC自动处理回收,而Object-C本质上还是C语言,所以很多时候还是需要手动去管理内存回收. 1. Object-C生成一 ...
- 最具有性价比的语言javascript之二,学习篇
先祝福各位,国庆假期愉快!前面写了一篇 最具有性价比的语言javascript之介绍篇,引起了不少的争议. 有些人javascript吐槽,埋怨.我目前做的都是后台系统.每个人的工作经历和兴趣爱好不一 ...
- Print Spooler 服务自动停止
1)先在服务里停止并禁用Print Spooler : 2)删除此文件夹下的所有文件,C:\Windows\System32\spool\PRINTERS\ : 3)删除注册表 HKEY_LOCAL_ ...
- Andorid Studio 模块化开发相关配置
Andorid Studio 模块化开发相关配置 下面以宿主APP模块和Uer_Module模块为例: 第一步:在项目根目录gradle.properties配置文件中添加如下代码 isNeedUse ...