问题:

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

题意:有一块草坪,长为l,宽为w,在它的水平中心线上有n个位置可以安装喷水装置,各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆。求出最少需要的喷水装置个数。

思路:贪心算法,如下图所示,将其转化成线区间来做。

代码:

#include<stdio.h>
#include<string.h>
#include<queue>
#include<math.h>
#include<algorithm>
using namespace std;
struct node
{double x,y;double n,m;
} q[12220];
double zmh(node w,node t)
{return w.n<t.n;
}
int main()
{int a,b,c;while(~scanf("%d%d%d",&a,&b,&c)){double k=c/2.0;int j=0;for(int i=0; i<a; i++){scanf("%lf%lf",&q[i].x,&q[i].y);if(q[i].y*2>c)//直径小于长方形的宽度舍去{double z=sqrt(q[i].y*q[i].y-k*k);q[j].n=q[i].x-z;q[j++].m=q[i].x+z;}}sort(q,q+j,zmh);int ans=0;if(q[0].n>0){printf("-1\n");continue;}int flag;double l=0,r=0;int tag[11000]= {0},u;while(1){flag=0;for(int k=0; k<j; k++){if(tag[k]==0&&q[k].n<=l&&q[k].m>=r){flag=1;r=q[k].m;//更新右边的值if(r>=b)flag=2;u=k;}}if(flag==1||flag==2){ans=ans+1;l=r;tag[u]=1;}elsebreak;if(flag==2)break;}if(r<b)flag=0;if(flag==0)printf("-1\n");else printf("%d\n",ans);}return 0;
}

Watering Grass UUV 1038 贪心相关推荐

  1. Watering Grass UVA - 10382 贪心

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

  2. UVA10382 - Watering Grass 题解

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

  3. UVa 10382 - Watering Grass

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

  4. 10382 - Watering Grass(贪心 区间覆盖问题)洒水面覆盖

    double qiuzhi(int id) {double t1=cc[id].rid*cc[id].rid;double t2=w*w/4;double t3=t1-t2;double t4=sqr ...

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

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

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

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

  7. UVa-10382 Watering Grass **

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

  8. Watering Grass——UVA10382

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

  9. uva10382 - Watering Grass

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

最新文章

  1. 服务器同步什么文件类型,不同服务器同步文件类型
  2. 【Raspberry Pi】定时运行python程序读温湿度传感器数据发邮件
  3. springboot中的过滤器、拦截器、监听器整合使用
  4. 对于一个ul列表,单击弹出每个li对应的索引
  5. PoE交换机的选择和使用注意事项介绍
  6. Js获取字符串的显示宽度/高度
  7. xos详解5:PendSV_Handler
  8. 计算机应用基础-0025,【技工学校《计算机应用基础》课程教学特点及讲授策略】 计算机应用基础...
  9. 【HCIE-RS 天梯路】MSDP
  10. 用慧编程做计算机,慧编程人工智能应用, 比个手势就能做算术!
  11. git从本地添加项目到远程仓库
  12. 华硕无线路由打印机服务器,彻底了解WL-500g型的华硕无线网络路由器
  13. 语义分割之边缘准确率提升
  14. 信息安全技术:健康医疗数据安全指南思维导图
  15. int a是神么意思
  16. 做公关就是做产品:新媒体时代的公关怎么做
  17. 【汽车总线技术】CAN和CAN FD总线故障诊断
  18. ​蔚来高速换电站,大家还没看懂的护城河
  19. VS错误:未能加载文件或程序集“NPOI, Version=2.3.0.0, Culture=neutral, PublicKeyToken...
  20. R7F0C901B2DNP-C#HA016位元RL78内核

热门文章

  1. Markdown语法学习笔记
  2. k8s集群搭建-1mater2node
  3. DW怎么把两个html放在一起,用Dreamweaver怎么制作网页
  4. c/c++ 计算屏幕的PPI
  5. OSChina 周二乱弹 ——人和人之间的关系用通话时间来表述
  6. 用数学计算1.01的365次方等于37.8来说明积跬步至千里是否合理? 1.01^365=37.8 0.99^365=0.03
  7. HPE矛头直指思科 与Arista达成软件定义合作关系及销售协议
  8. 文件系统测试工具整理
  9. leetcode---1758.生成交替二进制字符串的最少操作数
  10. benchmark datasets是什么