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

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.

1)如果圆的半径r*2<=草坪的宽度w,直接忽略该圆。

(2)对所有要考虑的圆按该圆与草坪上边界的的左交点的x坐标从小到大排序。

(3)如果所有圆能覆盖整个草坪,等价于圆与草坪的上边界交点所形成的区间覆盖了整个长度l。

(4)排完序后的圆,如果第一个的左交点x大于0,则无解。

(5)贪心选择圆的左交点不大于某个参考值,而右交点却最大的那个。(这里的参考值,一开始为0,如果找到了右交点最大的那个圆,就更新它)另外,如果找不到这样的圆,则无解。

(6)这里,圆的方程一般为(x-x0)^2+y^2=r^2,其中y=w/2.0,故不难得出,x=x0±sqrt(r^2-w^2/4.0)。

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;
struct M
{
double l,r;
}a[10020];
bool cmp(M a,M b)
{
if(a.l!=b.l) return a.l<b.l;
else return a.r>b.r;
}
int main()
{
std::ios::sync_with_stdio(false);
int n;
double len,w;
double c,rr;
while(cin>>n>>len>>w)
{
int num=0;
double maxr=-1.0;
for(int i=0;i<n;i++)
{
cin>>c>>rr;
if(rr*2<=w)
continue;
else
{
double l=c-sqrt(rr*rr-w*w/4);
double r=c+sqrt(rr*rr-w*w/4);
if(r>=maxr)
{
maxr=max(maxr,r);
}
a[num].l=l;
a[num++].r=r;
}
}
sort(a,a+num,cmp);
int k=0;
if(a[0].l>0||maxr<len)
{
cout<<-1<<endl;
continue;
}
double maxx=0;
int ans=0;
int flag=0;
int ww=0;
while(maxx<len)
{
double uu=maxx;
for(int i=0;i<num;i++)
{
if(a[i].l<=uu&&a[i].r>maxx)
maxx=a[i].r;
}
if(uu==maxx&&uu<len)
{
ww=1;
break;
}
ans++;
}
if(ww==0) cout<<ans<<endl;
else cout<<-1<<endl;
}
}

Watering Grass(贪心算法)的更多相关文章

  1. Watering Grass(贪心)

    Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long and w meters ...

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

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

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

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

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

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

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

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

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

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

  7. 贪心算法(Greedy Algorithm)

    参考: 五大常用算法之三:贪心算法 算法系列:贪心算法 贪心算法详解 从零开始学贪心算法 一.基本概念: 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以 ...

  8. 算法导论----贪心算法,删除k个数,使剩下的数字最小

    先贴问题: 1个n位正整数a,删去其中的k位,得到一个新的正整数b,设计一个贪心算法,对给定的a和k得到最小的b: 一.我的想法:先看例子:a=5476579228:去掉4位,则位数n=10,k=4, ...

  9. LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]

    Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...

  10. ACM_ICPC hdu-2111(简单贪心算法)

    一道非常简单的贪心算法,但是要注意输入的价值是单位体积的价值,并不是这个物品的总价值!#include <iostream> #include <stdio.h> #inclu ...

随机推荐

  1. Tensorflow创建已知分布的张量

    一.随机数 tf.random(num) 随机产生返回0----num-1的数 二.图变量 tf.Variable.init(initial_value, trainable=True, collec ...

  2. sublime python 去掉单行超出字数的白色框框 (E501)

    方法一 E501错误:行过长 (大于79个字符),在配置文件里设置 忽略E501错误即可 首选项-->Package Settings-->Anaconda-->Settings - ...

  3. js 判断用户是手机端还是电脑端访问

    通过userAgent 判断,网页可以直接使用 navigation对象 node端 可以通过请求头的 ctx.request.header['user-agent'] const browser = ...

  4. (二)数据源处理3-python处理包含合并单元格的excel

    分析:

  5. 修改机器的hostname

    vi /etc/sysconfig/network hostname=你想设置的主机名 不重启器的情况下使显示名称变成 hostname  主机名

  6. ctfhub技能树—彩蛋

    彩蛋题建议大家首先自己动手去找一找 做 好 准 备 后 再 看 下 文 !           1.首页 使用域名查询工具查询子域名 2.公众号 此题关注ctfhub公众号即可拿到,不过多赘述. 3. ...

  7. 1.2V升5V电源芯片,1.2V升3V的IC电路图方案

    镍氢电池就是典型的1.2V供电电源了,但是1.2V电压太低,需要电源芯片来1.2V升5V输出,或1.2V升3V输出稳压,1.2V单独难给其他芯片或者模块供电,即使串联1.2V*2=2.4V,也是因为电 ...

  8. MYSQL(将数据加载到表中)

    1. 创建和选择数据库 mysql> CREATE DATABASE menagerie; mysql> USE menagerie Database changed 2. 创建表 mys ...

  9. ModelForm的基本用法:

    一.ModelForm的基本用法示例: from django import forms from app01 import models class BookModelForm(forms.Mode ...

  10. Py变量,递归,作用域,匿名函数

    局部变量与全局变量 全局变量:全局生效的变量,在顶头的,无缩进的定义的变量. 局部变量:函数内生效的变量,在函数内定义的变量. name='1fh' def changename(): name='s ...