Watering Grass(贪心)
Watering Grass
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance from the left end of the center line and its radius of operation. What is the minimum number of sprinklers to turn on in order to water the entire strip of grass?

Input
Input consists of a number of cases. The first line for each case contains integer numbers n, l and w with n ≤ 10000. The next n lines contain two integers giving the position of a sprinkler and its radius of operation. (The picture above illustrates the first case from the sample input.)
Output
For each test case output the minimum number of sprinklers needed to water the entire strip of grass. If it is impossible to water the entire strip output ‘-1’.
Sample Input
8 20 2
5 3
4 1
1 2
7 2
10 2
13 3
16 2
19 4
3 10 1
3 5
9 3
6 1
3 10 1
5 3
1 1
9 1
Sample Output
6
2
-1
//意思是有 n 个喷水的,草地是长为 l 宽为 w 然后是 n 个喷水的东西,坐标和喷洒半径 p, r
首先要解决的问题是,如何表示一个喷洒的作用范围
L = p - sqrt ( r * r - w * w / 4 )
R = p - sqrt ( r * r - w * w / 4 )
实际完全覆盖了这个区间的草地
然后,按完全覆盖的最右距离递减排序,然后贪心,每次选出可以向右覆盖的最远喷洒即可
130ms
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; struct Node
{
double l,r;
/*
friend bool operator < (const Node&a,const Node&b)
{
return a.r > b.r;
}
*/ }node[]; void sort_node(int n)
{
for (int i=;i<n;i++)
{
int k=i;
for (int j=i+;j<n;j++)
{
if (node[j].r>node[k].r)
k=j;
}
swap(node[k],node[i]);
}
} int main()
{
int n;
while (cin>>n)
{
double L,W;
cin>>L>>W;
int i;
for (i=;i<n;i++)
{
double p,r;
cin>>p>>r;
node[i].l=p-sqrt(r*r-W*W/);
node[i].r=p+sqrt(r*r-W*W/);
}
//sort(node,node+n);//不知道为什么快排就是错的
sort_node(n);
double k=;
int num=;
while(k<L)
{
for (i=;i<n;i++)
{
if (node[i].l<=k&&node[i].r>k)
{
k=node[i].r;
num++;
break;
}
}
if (i==n) break;
}
if (k<L) cout<<"-1"<<endl;
else cout<<num<<endl;
}
return ;
}
Watering Grass(贪心)的更多相关文章
- UVA 10382 Watering Grass 贪心+区间覆盖问题
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...
- UVa 10382 - Watering Grass 贪心,水题,爆int 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- Watering Grass (贪心,最小覆盖)
参考: https://blog.csdn.net/shuangde800/article/details/7828675 https://www.cnblogs.com/haoabcd2010/p/ ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
- 10382 - Watering Grass
Problem E Watering Grass Input: standard input Output: standard output Time Limit: 3 seconds n sprin ...
- Watering Grass(贪心算法)
给定一条草坪.草坪上有n个喷水装置.草坪长l米宽w米..n个装置都有每个装置的位置和喷水半径..要求出最少需要几个喷水装置才能喷满草坪..喷水装置都是装在草坪中间一条水平线上的. n sprinkle ...
- UVA 10382 Watering Grass(区间覆盖,贪心)题解
题意:有一块草坪,这块草坪长l 米,宽 w 米,草坪有一些喷头,每个喷头在横坐标为 p 处,每个喷头的纵坐标都是(w/2) ,并且喷头的洒水范围是一个以喷头为圆心,半径为 r 米的圆.每次最少需要打开 ...
- UVa 10382 Watering Grass (区间覆盖贪心问题+数学)
题意:有一块长为l,宽为w的草地,在其中心线有n个喷水装置,每个装置可喷出以p为中心以r为半径的圆, 选择尽量少的装置,把草地全部润湿. 析:我个去啊,做的真恶心,看起来很简单,实际上有n多个坑啊,首 ...
- UVA 10382 Watering Grass (区间覆盖,贪心)
问题可以转化为草坪的边界被完全覆盖.这样一个圆形就换成一条线段. 贪心,从中选尽量少的线段把区间覆盖,按照把线段按左端点排序,记录一个当前已经覆盖区间的位置cur, 从左端点小于等于cur选一个右端点 ...
随机推荐
- ckeditor的详细配置(转)
CKEditor 3 JavaScript API Documentation : http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.con ...
- ES6中Set集合(与java里类似)
一.引入背景 Set集合是一种无重复元素的列表,开发者们一般不会逐一读取数组中的元素,也不太可能逐一访问Set集合中的每个元素,通常的做法是检测给定的值在某个集合中是否存在 Map集合内含多组键值对, ...
- ol 接入百度地图
ol5 如何接入百度地图,网上的资料很多,但是大多都有问题,在级别放大时,地图发生扭曲.为此注重研究了下ol5 接入百度地图的方法. 首先明确以下问题: 百度地图的投影是3857. 百度地图的分辨率和 ...
- iOS小技巧 - 为按钮设置不同状态下的背景色
我们知道直接在Storyboard中设置按钮的背景色是不能根据不同状态来更改的,那问题来了,如果我们需要在不同的状态下(比如按钮没有被按下或者被按下),使得按钮呈现不同的背景色怎么办? 比如上图左边是 ...
- [Angular] Angular ngSwitch Core Directive In Detail
When want to display different component based on some conditions: <div class='course-category' [ ...
- iOS_block代码块
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- Angular 学习笔记——自定义指令
<!DOCTYPE HTML> <html ng-app="myApp"> <head> <meta http-equiv="C ...
- IOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 转载
http://blog.csdn.net/he_jiabin/article/details/48677911 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为 ...
- WordPress安全检测工具
Wordpres是一款非常流行cms系统,市面上有相当一部分的博客和站点都是使用wordpress搭建.正因为大多博客都使用wordpress,所以其安全性就显得尤为重要. 我们平时维护一个站点(不仅 ...
- 为什么JVM指定-Xmx参数后占用内存会变少?
嘿,你能顺便过来看看这个奇怪的事情吗?” 就是让我提供支持的这个事情,驱使我写下这篇博客的.这个特殊的问题是,不同工具给出的可用内存的报告是不一样的. 简而言之,工程师正在调查特定应用程序的内存使用. ...