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.喷水装置(一)-贪心


UVA10382-Watering Grass-贪心 NYOJ6-喷水装置(一)-贪心的更多相关文章

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

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

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

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

  3. Watering Grass(贪心)

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

  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. The 10th Shandong Provincial Collegiate Programming Contest H.Tokens on the Segments(贪心+优先级队列 or 贪心+暴力)

    传送门 •题意 二维平面上有 n 条线段,每条线段坐标为 $(l_i,i),(r_i,i)$: 平面上的每个整点坐标上都可以放置一枚硬币,但是要求任意两枚硬币的横坐标不相同: 问最多有多少条线段可以放 ...

  7. Watering Grass(贪心算法)

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

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

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

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

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

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

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

随机推荐

  1. LINUX:alias命令详解

    发现目前安装的g++并没有开启选项 -std=c++11,无法使用c++11的新标准及其中的列表初始化.搜索后得到解决方法:键入:alias  g++="g++ -std=c++11&quo ...

  2. 数据库入门(以MySQL为例)

    一.数据库中的概念 1.数据库是用户存放数据.访问数据.操作数据的存储仓库,用户的各种数据被有组织地存放在数据库中.可以随时被有权限的用户查询.统计.添加.删除.和修改.可以说,数据库是长期存储在计算 ...

  3. DNS查询相关

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/45 一种简单的设计方式是在因特网上使用一个DNS服务器,该服务器 ...

  4. OpenStack运维(四):OpenStack备份恢复

    1.备份注意事项 要保留多少备份? 是否需要异地备份? 备份间隔多久? 恢复策略? 2.备份什么 2.1 数据库备份 2.1.1  制定crond 每天备份一次 2.1.2 备份命令根据系统而定,可用 ...

  5. java复写equals例子

    public class users { String name; static int age; public boolean equals(Object obj) { if(this==obj){ ...

  6. Webpack 2 视频教程 019 - Webpack 2 中配置多页面编译

    原文发表于我的技术博客 这是我免费发布的高质量超清「Webpack 2 视频教程」. Webpack 作为目前前端开发必备的框架,Webpack 发布了 2.0 版本,此视频就是基于 2.0 的版本讲 ...

  7. rsync服务器的搭建

    Rsync(remote synchronize)是一个远程数据同步工具,简要的概括就是主机于主机之间的文件目录数据的一个同步.下面就是rsync服务器的搭建过程.    系统环境 平台:Centos ...

  8. python科学计算_numpy_ndarray

    ndarray:n-dimensional array object,即多维数组对象,是python自带的array对象的扩展,array对象和list对象的区别是array对象的每一个元素都是数值, ...

  9. Tableau的简单数据可视化操作

    本文将讲解Tableau的基本使用和简单的数据分析. 在Tableau首页,我们可以看到有多种连接方式:文本文件.Excel.JSON文件.数据库等. 1.连接文本文件 点击"连接" ...

  10. Mock拦截请求URL返回模板数据

    背景 : 前后端开发依赖后端数据, 当前端页面开发完成 ,后端在没有提供前端数据的情况下 ,前端无法测试, 导致开发效率低 ,速度慢 ,为了解决这一问题 ,通过Mock模拟生成数据在不改变原有代码前提 ...