题目

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 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 Output 6 2 -1

题目大意

最小区间覆盖

eof结束

给定n l w分别是 数量 长度 宽度

n次输入 每次输入喷壶的位置和半径

然后求最少个能覆盖的喷壶数

算法: 贪心 最小区间覆盖

代码

/*给定一个端点n,再给一定数量的区间[li,ri] ,问覆盖[0,n]( m初始为0 )最少需要多少个区间,如果不能输出0。。
为了能使一个区间覆盖的更多,将所有区间按右端点降序排序,如果一个区间[l,r] 覆盖了start ,
则说明[0,r]已经被覆盖,所以令start=r,剩下需要被覆盖的为[r,n] (在r<n的情况下,若r>=n,则说明[m,n]被完全覆盖,直接结束)
*/
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
struct M{double start;double end;
}q[10005];double cmp (M a,M b) //按最大能覆盖到排序
{return a.end>b.end;
}
int main()
{double l,w,p,r;double start,end;int n,i,qn,outn;while(cin>>n>>l>>w){outn=0;start=0.0;end=l;qn=0;for(i=0;i<n;i++){cin>>p>>r;if(2*r>w){q[qn].start=p-sqrt(r*r-w*w/4);q[qn].end=p+sqrt(r*r-w*w/4);qn++;}}sort(q,q+qn,cmp);while(start<end){for(i=0;i<qn;i++) {if(q[i].start<=start && q[i].end>start ){start=q[i].end;          //更新区间outn++;break;}}if(i==qn) break;       //如果没有一个满足条件的区间,直接结束。}if(start<end) cout<<"-1"<<endl;else cout<<outn<<endl;}return 0;
}

相关看 https://blog.csdn.net/baidu_41907100/article/details/84673955

注意

一开始我的一直WA 然后百度对比之后发现不同点在于

也就是判断圆直径是否大于宽度的顺序不同 我实在是不知道为什么顺序不同如何导致的错误

附上错误代码

/*给定一个端点n,再给一定数量的区间[li,ri] ,问覆盖[0,w]( m初始为0 )最少需要多少个区间,如果不能输出0。。
为了能使一个区间覆盖的更多,将所有区间按右端点降序排序,如果一个区间[l,r] 覆盖了start ,
则说明[0,r]已经被覆盖,所以令start=r,剩下需要被覆盖的为[r,n] (在r<n的情况下,若r>=n,则说明[m,n]被完全覆盖,直接结束)
*/
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
struct M{double p;double r; double start;double end;
}q[10005];double cmp (M a,M b) //按最大能覆盖到排序
{//return a.r>b.r;return a.end>b.end;
}
int main()
{double l,w;double start,end;int n,i,outn;while(cin>>n>>l>>w){outn=0;start=0.0;end=l;for(i=0;i<n;i++){cin>>q[i].p>>q[i].r;q[i].start=q[i].p-sqrt(q[i].r*q[i].r-(w/2)*(w/2));q[i].end=q[i].p+sqrt(q[i].r*q[i].r-(w/2)*(w/2));}sort(q,q+n,cmp);while(start<end){for(i=0;i<n;i++) {if(q[i].start<=start && q[i].end>start && 2*q[i].r>w){start=q[i].end;            //更新区间outn++;break;}}if(i==n) break;        //如果没有一个满足条件的区间,直接结束。}if(start<end) cout<<"-1"<<endl;else cout<<outn<<endl;}return 0;
}

Watering Grass——UVA10382相关推荐

  1. UVA10382 - Watering Grass 题解

    原题链接 Online Judge: 10382 - Watering Grass Virtual Judge: Watering Grass - UVA 10382 洛谷:UVA10382 Wate ...

  2. UVa 10382 - Watering Grass

    链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&pa ...

  3. UVa-10382 Watering Grass **

    /* * Uva-10382-Watering Grass.cpp * 特别注意精度..(感觉这道题的判题有问题,开始怎么交都WA,,过两天什么都没改,再交就AC了 , 汗.. * * 详细代码注释: ...

  4. UVa10382 - Watering Grass(贪心算法)

    问题:给出一个长为l,宽为w的绿化带,n个喷水装置及其对就位置x和影响半径r.问最少需要多少个喷水装置能全覆盖此绿化带 思路:首先要计算喷水装置可以覆盖的区间,如果喷水装置的影响半径小于等于w/2,是 ...

  5. UVA-10382 Watering Grass

    题意:有一个长方形草坪,长为l,宽为w,在它的水平中心线上可以安装n个喷水器来为草坪浇水,每个喷水器能够灌溉的范围是以该喷水器为中心的半径为r的圆.我们要灌溉整个草坪,问你最少需要的喷水器数. 题解: ...

  6. uva10382 - Watering Grass

    题目大意:输入n,l,w.分别代表有n个喷水装置,草地长为L,宽为W:接下来n行,每行两个数,a,r,分别代表喷水装置在草地中的横坐标,和喷水半径.喷水装置的纵坐标都是草地的正中央.求用最少的喷水装置 ...

  7. Watering Grass UUV 1038 贪心

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

  8. Watering Grass UVA - 10382 贪心

    问题 https://vjudge.net/problem/UVA-10382 分析 将一个点的覆盖范围看作是一个长方形,舍弃弓形区域,变成区间覆盖问题,用贪心法 注意:bb-ww/4有可能小于0,要 ...

  9. *【UVA - 10382】Watering Grass(贪心,区间覆盖问题,思维)

    题干: 题目大意: 有一块草坪,长为l,宽为w,在它的水平中心线上有n个位置可以安装喷水装置,各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆.求出最少需要的喷水装置个数,如果无论如何都不能覆 ...

最新文章

  1. 【C++】模板函数的声明和定义必须在同一个文件中
  2. 强势安利5款高质量办公软件,极大提高办公效率
  3. PHP公鸡五文钱,公鸡
  4. java 远程调用url_使用Java的URL/HttpURLConnection进行远程调用(POST请求)
  5. linux如何敲打中文,vim敲字如弹琴 - linux-tao的个人空间 - OSCHINA - 中文开源技术交流社区...
  6. 如何才能学好javascript
  7. has install-snap change in progress问题
  8. MySQL索引原理及慢查询优化,了解一下?
  9. linux系统调用使用方法,Linux系统的使用以及系统调用的开发方法OS.ppt
  10. java文件转码工具-native2ascii.exe命令简介
  11. 【音视频】使用DXGI实现多屏幕采集(4-2)
  12. T检验和p-value含义及计算公式
  13. GP数列 三角形斜边 小码哥的生日 完全平方数
  14. 自制Alfred/Wox插件推荐
  15. 笔记本加装SSD重装系统win10引导始终指向原系统的问题终极解决方法
  16. Swing 美化包-JTattoo
  17. OpenCv-C++-SURF特征检测
  18. 游戏分类&&游戏开发常用术语
  19. 矩阵分析与应用学习笔记1
  20. Retrofit流程及设计模式全解析

热门文章

  1. origin pro 2021去水印
  2. MIT一牛人对数学在机器学习中的作用给的评述
  3. sublime text 3经典颜色主题-Soda
  4. Ubuntu USB设备端口号绑定
  5. 新海诚没有参与制作的作品_全能的新海诚,最初几部作品都是他一个人完成的!但是却有缺陷!...
  6. alpha测试和beta测试Gamma测试的区别是什么?
  7. 软件测试:Alpha测试与beta测试区别
  8. python获取股票的市盈率_怎样查找股票的历史市盈率数据?
  9. OpenCV极坐标转换函数warpPolar的使用
  10. aliyun mysql 端口_阿里云怎么查看数据库端口怎么设置