Watering Grass(贪心算法)
给定一条草坪。草坪上有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(贪心算法)的更多相关文章
- Watering Grass(贪心)
Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long and w meters ...
- 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 ...
- UVa 10382 Watering Grass (区间覆盖贪心问题+数学)
题意:有一块长为l,宽为w的草地,在其中心线有n个喷水装置,每个装置可喷出以p为中心以r为半径的圆, 选择尽量少的装置,把草地全部润湿. 析:我个去啊,做的真恶心,看起来很简单,实际上有n多个坑啊,首 ...
- 贪心算法(Greedy Algorithm)
参考: 五大常用算法之三:贪心算法 算法系列:贪心算法 贪心算法详解 从零开始学贪心算法 一.基本概念: 所谓贪心算法是指,在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以 ...
- 算法导论----贪心算法,删除k个数,使剩下的数字最小
先贴问题: 1个n位正整数a,删去其中的k位,得到一个新的正整数b,设计一个贪心算法,对给定的a和k得到最小的b: 一.我的想法:先看例子:a=5476579228:去掉4位,则位数n=10,k=4, ...
- 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 ...
- ACM_ICPC hdu-2111(简单贪心算法)
一道非常简单的贪心算法,但是要注意输入的价值是单位体积的价值,并不是这个物品的总价值!#include <iostream> #include <stdio.h> #inclu ...
随机推荐
- Synchronized 精讲
1.简介 1.1 作用 在并发场景中,保证同一时刻只有一个线程对有并发隐患的代码进行操作 1.2 错误案例 需求:两个线程对 count 变量进行200000次循环增加,预期结果是400000次 pu ...
- springboot源码解析-管中窥豹系列之aware(六)
一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...
- 浅谈sql索引
索引是什么 假如你手上有一个你公司的客户表,老板说找什么客户你就得帮他找出来. 客户不多的时候,你拿着手指一行一行滑,费不了多少时间就能找到. 后来公司做大了,客户越来越多,好几页的客户,你发现,一行 ...
- bash shell关联数组总结
[原创]本博文为原创博文,引用或转发请注明原始出处和链接:https://www.cnblogs.com/dingbj/p/dict_array.html 什么是关联数组? 关联数组相对于索引数组,又 ...
- 干货!上古神器 sed 教程详解,小白也能看的懂
目录: 介绍工作原理正则表达式基本语法数字定址和正则定址基本子命令实战练习 介绍 熟悉 Linux 的同学一定知道大名鼎鼎的 Linux 三剑客,它们是 grep.awk.sed,我们今天要聊的主角就 ...
- 数字化转型中企业真正困惑-传统IT架构如何改造和全面上云
对数字化转型,整体来看大部分人相对关心问题主要还是集中在以下两个方面. 企业传统的IT架构如何如何微服务改造,演进发展 企业传统IT如何全面上云和实施云原生 以上两点实际都包括一个关键点,即企业当前内 ...
- 一文搞定全场景K3s离线安装
作者简介 王海龙,Rancher中国社区技术经理,负责Rancher中国技术社区的维护和运营.拥有6年的云计算领域经验,经历了OpenStack到Kubernetes的技术变革,无论底层操作系统Lin ...
- 写给 Poppy 的 MySQL 速查表
昨天 Poppy 问我是不是应该学一些网页开发的东西, 我的回答是这样的: 今天花了点时间汇总了一些 MySQL 简单的命令. ======== 正文分割线 ======== 有哪些常见的数据库: O ...
- HA工作机制
HA工作机制 HA:高可用(7*24小时不中断服务) 主要的HA是针对集群的master节点的,即namenode和resourcemanager,毕竟DataNode挂掉之后影响 不是特别大,重启就 ...
- 如何用OKR促进跨团队协同
https://mp.weixin.qq.com/s/347dKRlez0_KJKGOkTI0AQ