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

相关文章

解决Maven项目idea找不到本地仓库jar包问题以及使用mvn install:install-file

《解决Maven项目idea找不到本地仓库jar包问题以及使用mvninstall:install-file》:本文主要介绍解决Maven项目idea找不到本地仓库jar包问题以及使用mvnin... 目录Maven项目idea找不到本地仓库jar包以及使用mvn install:install-file基

最详细安装 PostgreSQL方法及常见问题解决

《最详细安装PostgreSQL方法及常见问题解决》:本文主要介绍最详细安装PostgreSQL方法及常见问题解决,介绍了在Windows系统上安装PostgreSQL及Linux系统上安装Po... 目录一、在 Windows 系统上安装 PostgreSQL1. 下载 PostgreSQL 安装包2.

usb接口驱动异常问题常用解决方案

《usb接口驱动异常问题常用解决方案》当遇到USB接口驱动异常时,可以通过多种方法来解决,其中主要就包括重装USB控制器、禁用USB选择性暂停设置、更新或安装新的主板驱动等... usb接口驱动异常怎么办,USB接口驱动异常是常见问题,通常由驱动损坏、系统更新冲突、硬件故障或电源管理设置导致。以下是常用解决

Maven如何手动安装依赖到本地仓库

《Maven如何手动安装依赖到本地仓库》:本文主要介绍Maven如何手动安装依赖到本地仓库问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、下载依赖二、安装 JAR 文件到本地仓库三、验证安装四、在项目中使用该依赖1、注意事项2、额外提示总结一、下载依赖登

Mysql如何解决死锁问题

《Mysql如何解决死锁问题》:本文主要介绍Mysql如何解决死锁问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录【一】mysql中锁分类和加锁情况【1】按锁的粒度分类全局锁表级锁行级锁【2】按锁的模式分类【二】加锁方式的影响因素【三】Mysql的死锁情况【1

SpringBoot内嵌Tomcat临时目录问题及解决

《SpringBoot内嵌Tomcat临时目录问题及解决》:本文主要介绍SpringBoot内嵌Tomcat临时目录问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录SprinjavascriptgBoot内嵌Tomcat临时目录问题1.背景2.方案3.代码中配置t

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

如何在Mac上安装并配置JDK环境变量详细步骤

《如何在Mac上安装并配置JDK环境变量详细步骤》:本文主要介绍如何在Mac上安装并配置JDK环境变量详细步骤,包括下载JDK、安装JDK、配置环境变量、验证JDK配置以及可选地设置PowerSh... 目录步骤 1:下载JDK步骤 2:安装JDK步骤 3:配置环境变量1. 编辑~/.zshrc(对于zsh

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、