10382 - Watering Grass

Time limit: 3.000 seconds

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

0 2

13 3

16 2

19 4

3 10 1

3 5

9

6 1

3 10 1

5 3

1 1

9 1
Sample Output
6

2

-1

题意就是浇水,整片草地都要浇上水,装置在草地水平中间的线上,要注意的是不是圆的面积,是近似矩形的面积。

然后就是直接算长度就可以,要求装置洒水的半径要>草地一半的宽度。

然后长度的话也不是圆的半径,是图上红色的长度。(图画大了,挠头。。。)

关键就是左右两边的长度计算,精度很重要,打训练赛的时候就是卡死在精度这里,wa了8发,gg。

a[i].l=max(0.0,w-sqrt((double)r*r-(double)s*s/4.0));
a[i].r=min((double)l,w+sqrt((double)r*r-(double)s*s/4.0));

就是这个,还是一个大佬帮我改对的。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
struct node{
double l,r;
}a[N];
double cmp(node a,node b){
return a.r>b.r;
}
int main(){
int n,num,l,s,w,r;
double p;
while(~scanf("%d%d%d",&n,&l,&s)){
p=0.0;num=;
for(int i=;i<n;i++){
scanf("%d%d",&w,&r);
if(r>=s/2.0){
a[i].l=max(0.0,w-sqrt((double)r*r-(double)s*s/4.0)); //wa在这里了。
a[i].r=min((double)l,w+sqrt((double)r*r-(double)s*s/4.0));}
}
sort(a,a+n,cmp);
//for(int i=0;i<n;i++) cout<<a[i].l<<" "<<a[i].r<<endl;
while(p<l){
int i;
for(i=;i<n;i++){
if(a[i].l<=p&&a[i].r>p){
p=a[i].r;
num++;
break;
}
}
if(i==n)break;
}
if(p<l) printf("-1\n");
else printf("%d\n",num);
}
return ;
}

网上好多代码都是错的,卡精度上了。

==

然后就是NYOJ上有一道贪心长得差不多,但是比上面的水。。。

NYOJ 6.喷水装置(一)-贪心


UVA 10382.Watering Grass-贪心的更多相关文章

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

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

  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【贪心+区间覆盖问题+高精度】

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

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

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

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

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

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

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

  7. UVa 10382 - Watering Grass

    题目大意:有一条长为l,宽为w的草坪,在草坪上有n个洒水器,给出洒水器的位置和洒水半径,求能浇灌全部草坪范围的洒水器的最小个数. 经典贪心问题:区间覆盖.用计算几何对洒水器的覆盖范围简单处理一下即可得 ...

  8. UVA 10382 Watering Grass(区间覆盖)

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

  9. uva 10382 - Watering Grass(区域覆盖问题)

    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 ...

  10. uva 10382 Watering Grass_贪心

    题意:给你个矩形n*m,再给你n个圆的圆心坐标和半径,问最用最少用几个圆把这个矩形覆盖 思路:直接想发现这问题不容易,后来发现可以把圆看做区间(能把矩形面积覆盖),然后这个问题就容易解决了 #incl ...

随机推荐

  1. ES mapping field修改过程

    Elasticsearch 的坑爹事--记录一次mapping field修改过程 http://www.cnblogs.com/Creator/p/3722408.html Elasticsearc ...

  2. WCF分布式开发步步为赢(13):WCF服务离线操作与消息队列MSMQ

    之前曾经写过一个关于MSMQ消息队列的文章:WCF分布式开发必备知识(1):MSMQ消息队列 ,当时的目的也是用它来作为学习WCF 消息队列MSMQ编程的基础文章.在那篇文章里,我们详细介绍了MSMQ ...

  3. c++编辑器下载地址

    https://msdn.itellyou.cn/ 输入上述地址选中下图所示的按钮:

  4. sls语法:创建file,创建文件夹

    http://blog.kukafei520.net/html/2014/942.html /tmp/aaa.txt: file.managed /tmp/salt_test: file.direct ...

  5. HTML5 视频直播

    目前视频直播,尤其是移动端的视频直播已经火到不行了,基本上各大互联网公司都有了自己的直播产品,所以对于直播的一些基本知识和主要技术点也要有所了解,本次分享就向大家介绍一下其中的奥秘. 内容大体框架:  ...

  6. PHP 抽象类,接口,抽象方法,静态方法

    1.Abstract class(抽象类) 抽象类是指在 class 前加了 abstract 关键字且存在抽象方法(在类方法 function 关键字前加了 abstract 关键字)的类. 抽象类 ...

  7. kdtree学习记录

    [转载请注明来自 Galaxies的博客:http://cnblogs.com/galaxies] 这篇文章当做一个记录啦qwq 参考:<K-D Tree在信息学竞赛中的应用>(n+e, ...

  8. [POJ1595]欧拉线性筛(虽然这道题不需要...)

    欧拉线性筛. 对于它的复杂度的计算大概思考了很久. procedure build_prime; var i,j:longint; begin fillchar(vis,sizeof(vis),tru ...

  9. 【洛谷 P4886】 快递员 (点分治)

    这题因为一些小细节还是\(debug\)了很久...不过我第一次用脚本对拍,不亏. 先随便找一个点作为根,算出答案,即所有点对到这个点的距离和的最大值,并记录所有距离最大的点对.如果这个点在任意一个距 ...

  10. bzoj 2152 点剖分

    比较裸的点剖分,访问到每个重心时,记录一个b数组, 代表当前子树中长度为i的边的数量是b[i],因为是3的倍数, 所以边长mod 3保存就行了,然后记录一个sum数组,代表 当前子树中一个子节点为根的 ...