POJ 1328 Radar Installation 雷达安装 贪心问题求解

2024-06-08 10:48

本文主要是介绍POJ 1328 Radar Installation 雷达安装 贪心问题求解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目链接: POJ 1328 Radar Installation

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 11 2
0 20 0

Sample Output

Case 1: 2
Case 2: 1

Source

Beijing 2002

题意

在x轴表示的海岸线上需要布置雷达,以覆盖海中的n个海岛,雷达的覆盖半径为d,求解如何选择布置点才能使用到的雷达最少。如果有的海岛不能被覆盖到,那么输出-1。

分析

考察以海岛为圆心,做半径为d的圆,看与x轴相交的那段区间,这样的话,这个区间内的任何位置布置雷达,都是可以覆盖这个海岛的,对于所有的海岛,当然不乏求到的区间有部分重合的情况,那么在这个重合的区间中布置雷达,当然就能覆盖到两个以上的点,这样就能节省雷达,雷达所在的区间越多,节省的雷达就越多。


思想

典型的贪心思想。对于求出的每一个区间,我们进行排序,让区间右端点小的排在前面,如果右端点相等,那么左端点大的排在前面(想想为什么)。

那么该如何选择布置点呢?

首先我们选取排序号后的第一个区间的右端点为第一个布置点,它的位置为st,然后再按顺序找后面的区间。如果当前区间的左端点大于st,说明st位置的雷达不能覆盖到这个海岛,那么雷达数加1,同时更新st为这个区间的右端点。如果当前区间的左端点小于等于st,则说明st位置的雷达能覆盖这个海岛,忽略这个区间。

代码

/*POJ_1328_Radar_InstallationAuthor: Sign_Greedy
*/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;int n, d;
struct seg
{double l, r;
}SEG[1010];seg pos(int x, int y)
{seg s;s.l = x - sqrt(d*d - y*y);s.r = x + sqrt(d*d - y*y);return s;
}
bool cmp(seg a, seg b)
{if(a.r == b.r) return a.l > b.l;return a.r < b.r;
}
int main()
{int x[1010], y[1010], cas = 1;while(scanf("%d%d", &n, &d), n, d){bool flag = true;for(int i = 0; i < n; i++){scanf("%d%d", &x[i], &y[i]);if(y[i] > d)flag = false;}if(!flag){printf("Case %d: -1\n", cas++);continue;}for(int i = 0; i < n; i++)SEG[i] = pos(x[i], y[i]);sort(SEG, SEG+n, cmp);int cnt = 1;double st = SEG[0].r;for(int i = 1; i < n; i++)if(SEG[i].l > st){st = SEG[i].r;cnt++;}printf("Case %d: %d\n", cas++, cnt);}return 0;
}



这篇关于POJ 1328 Radar Installation 雷达安装 贪心问题求解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1041969

相关文章

从零教你安装pytorch并在pycharm中使用

《从零教你安装pytorch并在pycharm中使用》本文详细介绍了如何使用Anaconda包管理工具创建虚拟环境,并安装CUDA加速平台和PyTorch库,同时在PyCharm中配置和使用PyTor... 目录背景介绍安装Anaconda安装CUDA安装pytorch报错解决——fbgemm.dll连接p

解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题

《解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题》在Spring开发中,@Autowired注解常用于实现依赖注入,它可以应用于类的属性、构造器或setter方法上,然... 目录1. 为什么 @Autowired 在属性上被警告?1.1 隐式依赖注入1.2 IDE 的警告:

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

mac安装nvm(node.js)多版本管理实践步骤

《mac安装nvm(node.js)多版本管理实践步骤》:本文主要介绍mac安装nvm(node.js)多版本管理的相关资料,NVM是一个用于管理多个Node.js版本的命令行工具,它允许开发者在... 目录NVM功能简介MAC安装实践一、下载nvm二、安装nvm三、安装node.js总结NVM功能简介N

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

关于Nginx跨域问题及解决方案(CORS)

《关于Nginx跨域问题及解决方案(CORS)》文章主要介绍了跨域资源共享(CORS)机制及其在现代Web开发中的重要性,通过Nginx,可以简单地解决跨域问题,适合新手学习和应用,文章详细讲解了CO... 目录一、概述二、什么是 CORS?三、常见的跨域场景四、Nginx 如何解决 CORS 问题?五、基

python安装whl包并解决依赖关系的实现

《python安装whl包并解决依赖关系的实现》本文主要介绍了python安装whl包并解决依赖关系的实现,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、什么是whl文件?二、我们为什么需要使用whl文件来安装python库?三、我们应该去哪儿下

MySQL安装时initializing database失败的问题解决

《MySQL安装时initializingdatabase失败的问题解决》本文主要介绍了MySQL安装时initializingdatabase失败的问题解决,文中通过图文介绍的非常详细,对大家的学... 目录问题页面:解决方法:问题页面:解决方法:1.勾选红框中的选项:2.将下图红框中全部改为英

MySQL9.0默认路径安装下重置root密码

《MySQL9.0默认路径安装下重置root密码》本文主要介绍了MySQL9.0默认路径安装下重置root密码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录问题描述环境描述解决方法正常模式下修改密码报错原因问题描述mysqlChina编程采用默认安装路径,

Nginx启动失败:端口80被占用问题的解决方案

《Nginx启动失败:端口80被占用问题的解决方案》在Linux服务器上部署Nginx时,可能会遇到Nginx启动失败的情况,尤其是错误提示bind()to0.0.0.0:80failed,这种问题通... 目录引言问题描述问题分析解决方案1. 检查占用端口 80 的进程使用 netstat 命令使用 ss