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(贪心)的更多相关文章

  1. UVA 10382 Watering Grass 贪心+区间覆盖问题

    n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...

  2. UVa 10382 - Watering Grass 贪心,水题,爆int 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  3. Watering Grass (贪心,最小覆盖)

    参考: https://blog.csdn.net/shuangde800/article/details/7828675 https://www.cnblogs.com/haoabcd2010/p/ ...

  4. UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】

    UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...

  5. 10382 - Watering Grass

    Problem E Watering Grass Input: standard input Output: standard output Time Limit: 3 seconds n sprin ...

  6. Watering Grass(贪心算法)

    给定一条草坪.草坪上有n个喷水装置.草坪长l米宽w米..n个装置都有每个装置的位置和喷水半径..要求出最少需要几个喷水装置才能喷满草坪..喷水装置都是装在草坪中间一条水平线上的. n sprinkle ...

  7. UVA 10382 Watering Grass(区间覆盖,贪心)题解

    题意:有一块草坪,这块草坪长l 米,宽 w 米,草坪有一些喷头,每个喷头在横坐标为 p 处,每个喷头的纵坐标都是(w/2) ,并且喷头的洒水范围是一个以喷头为圆心,半径为 r 米的圆.每次最少需要打开 ...

  8. UVa 10382 Watering Grass (区间覆盖贪心问题+数学)

    题意:有一块长为l,宽为w的草地,在其中心线有n个喷水装置,每个装置可喷出以p为中心以r为半径的圆, 选择尽量少的装置,把草地全部润湿. 析:我个去啊,做的真恶心,看起来很简单,实际上有n多个坑啊,首 ...

  9. UVA 10382 Watering Grass (区间覆盖,贪心)

    问题可以转化为草坪的边界被完全覆盖.这样一个圆形就换成一条线段. 贪心,从中选尽量少的线段把区间覆盖,按照把线段按左端点排序,记录一个当前已经覆盖区间的位置cur, 从左端点小于等于cur选一个右端点 ...

随机推荐

  1. 天地图应用ArcGIS发布的服务(转)

    天地图应用ArcGIS发布的服务 本文包含三个部分:利用ArcMap将Excel的数据转化为ArcGIS MXD文件.利用ArcMap发布服务.天地图添加ArcGIS发布的服务. 一 MXD文件的生成 ...

  2. 机器学习第2课:单变量线性回归(Linear Regression with One Variable)

    2.1  模型表示 之前的房屋交易问题为例,假使我们回归问题的训练集(Training Set)如下表所示: 我们将要用来描述这个回归问题的标记如下: m                代表训练集中实 ...

  3. 配置Linux系统实现dhcp功能

    配置Linux系统实现dhcp功能 1.背景及原理    DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)通常被应用在大型的局域网络环境中,主要作用 ...

  4. SSO单点登录系列3:cas-server端配置认证方式实践(数据源+自定义java类认证)

    落雨 cas 单点登录 本篇将讲解cas-server端的认证方式 1.最简单的认证,用户名和密码一致就登录成功 2.配置Oracle的jdbc数据源,通过spring动态查询数据库 3.配置orac ...

  5. [Functional Programming] Define Discrete State Transitions using the State ADT

    We build our first state transactions as two discrete transactions, each working on a specific porti ...

  6. ANT使用 - 用for和foreach的方法遍历一个文件夹,查找到某个文件并删除

    转自:http://www.cnblogs.com/QAZLIU/p/3732329.html?utm_source=tuicool&utm_medium=referral build.xml ...

  7. 如何检测一个aspx页面的速度慢的原因

    最近读到一篇文章,是关于如何提高一个aspx页面的速度.这是一个常见的面试问题.该问题原文出自这个网站. 出现这个问题的原因会多种多样,我们需要一步一步的排查来定位问题真正出现在哪里. 1. 找出那一 ...

  8. HTML5实战与剖析之媒体元素(6、视频实例)

    HTML5中的视频标签和及其模仿视频播放器的效果在一些手机端应用比較多.由于手机端基本上废除了flash的独断.让HTML5当家做主人,所以对视频支持的比較好. 所以今天专门为大家奉上HTML5视频标 ...

  9. (四)Thymeleaf标准表达式之——[3->6] 操作符(文本、算术、布尔、比较及相等)

    2.3 文本操作符 模板名称:text.html 连接符: +   可以是任意字符和表达式等 文本替换符:|    不能表达出条件表达式(官网:只能是变量表达式) e.g. 1.<span th ...

  10. presentModalViewController方法,present一个透明的viewController,带动画效果

    //假设需要被present的控制器实例为controller,controller的背景色设置为clearColor UIViewController * rootcontroller = self ...