给定一条草坪。草坪上有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. SpringCloud | 通过电商业务场景让你彻底明白SpringCloud核心组件的底层原理

    本文分为两个部分: Spring Cloud"全家桶"简单介绍. 通过实际电商业务场景,让你彻底明白Spring Cloud几个核心组件的底层原理. Spring Cloud介绍 ...

  2. 常见大中型网络WLAN基本业务实例

    组网图形 大中型WLAN网络简介 本文介绍的WLAN网络是指利用频率为2.4GHz或5GHz的射频信号作为传输介质的无线局域网,相对于有线网络的铺设成本高,不便于网络调整和扩展.位置固定,移动性差等缺 ...

  3. 【C++】《C++ Primer 》第五章

    第五章 语句 一.简单语句 表达式语句:一个表达式末尾加上分号,就变成了表达式语句. 空语句:只有一个单独的分号,记得注释说明提高代码可读性. 复合语句(块):用花括号 {}包裹起来的语句和声明的序列 ...

  4. Flutter 基础组件:状态管理

    前言 一个永恒的主题,"状态(State)管理",无论是在React/Vue(两者都是支持响应式编程的Web开发框架)还是Flutter中,他们讨论的问题和解决的思想都是一致的. ...

  5. SwiftUI 官方画图实例详细解析

    前言 在前面几篇关于SwiftUI的文章中,我们用一个具体的基本项目Demo来学习了下SwiftUI,里面包含了常见的一些控件使用以及数据处理和地图等等,有兴趣的小伙伴可以去翻翻以前的文章,在前面总结 ...

  6. vagrant up报错【io.rb:32:in `encode': "\x95" followed by "\"" on GBK (Encoding::InvalidByteSequenceError)】

    vagrant up报错[io.rb:32:in `encode': "\x95" followed by """ on GBK (Encoding: ...

  7. pandas DataFrame的新增行列,修改、删除、筛选、判断元素以及转置操作

    1)指定行索引和列索引标签 index 属性可以指定 DataFrame 结构中的索引数组,  columns 属性可以指定包含列名称的行, 而使用 name 属性,通过对一个 DataFrame 实 ...

  8. Sentry(v20.12.1) K8S 云原生架构探索,JavaScript 性能监控之采样 Transactions

    系列 Sentry-Go SDK 中文实践指南 一起来刷 Sentry For Go 官方文档之 Enriching Events Snuba:Sentry 新的搜索基础设施(基于 ClickHous ...

  9. USB充电限流芯片,输出短路关闭,过压关闭

    PW1503,PW1502是超低RDS(ON)开关,具有可编程的电流限制,以保护电源源于过电流和短路保护.它具有超温保护以及反向闭锁功能. PW1503,PW1502采用薄型(1毫米)5针薄型SOT2 ...

  10. celery应用

    celery---分布式任务队列 Celery是一个简单,灵活且可靠的分布式系统,可以处理大量消息,同时为操作提供维护该系统所需的工具. Celery是一个基于python开发的模块,可以帮助我们对任 ...