UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】

简介: UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide.

UVa 10382 - Watering Grass

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 nl 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为圆。求出最少需要的喷水装置个数。

分析与总结:
这题的关键在于转化

根据这图可以看出,一个喷水装置的有效覆盖范围就是圆中间的那个矩形。所以,在输入的同时,进行预处理,转换成矩形左边的坐标和右边的坐标。 这样,其实就转换成了经典的区间覆盖问题。
下面给出AC代码:
  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 inline int read()
  4 {
  5     int x=0,f=1;
  6     char ch=getchar();
  7     while(ch<'0'||ch>'9')
  8     {
  9         if(ch=='-')
 10             f=-1;
 11         ch=getchar();
 12     }
 13     while(ch>='0'&&ch<='9')
 14     {
 15         x=x*10+ch-'0';
 16         ch=getchar();
 17     }
 18     return x*f;
 19 }
 20 inline void write(int x)
 21 {
 22     if(x<0)
 23     {
 24         putchar('-');
 25         x=-x;
 26     }
 27     if(x>9)
 28     {
 29         write(x/10);
 30     }
 31     putchar(x%10+'0');
 32 }
 33 struct Node
 34 {
 35     double x,y;
 36 }p[10005];
 37 int cmp(const Node &a,const Node &b)
 38 {
 39     if(a.x<b.x)
 40     return 1;
 41     else
 42     return 0;
 43 }
 44 int main()
 45 {
 46     int n;
 47     double l,w;
 48     while(scanf("%d%lf%lf",&n,&l,&w)!=EOF)
 49     {
 50         for(int i=0;i<n;i++)
 51         {
 52             double s,r;
 53             scanf("%lf%lf",&s,&r);
 54             if(r<w/2.0-1e-6||fabs(r-w/2.0)<1e-6)
 55             {
 56                 i--;
 57                 n--;
 58                 continue;
 59             }
 60             double x=sqrt(r*r-w*w/4.0);
 61             p[i].x=s-x;
 62             p[i].y=s+x;
 63             if(p[i].x<1e-6)
 64             p[i].x=0;
 65             if(p[i].y>l+1e-6)
 66             p[i].y=l;
 67         }
 68         sort(p,p+n,cmp);
 69         if(fabs(p[0].x)>1e-6)
 70         {
 71             printf("-1\n");
 72             continue;
 73         }
 74         double pos=0,maxx=0;
 75         int sum=0;
 76         while(1)
 77         {
 78             if(pos>=l)
 79             break;
 80             maxx=0;
 81             for(int i=0;i<n;i++)
 82             if((p[i].x<pos-1e-6||fabs(p[i].x-pos)<1e-6)&&p[i].y>pos+1e-6)
 83             {
 84                 if(p[i].y>maxx+1e-6)
 85                 {
 86                     maxx=p[i].y;
 87                 }
 88             }
 89             if(fabs(maxx)<1e-6)
 90             {
 91                 sum=0;
 92                 break;
 93             }
 94             sum++;
 95             pos=maxx;
 96         }
 97         if(sum==0)
 98         printf("-1\n");
 99         else
100         printf("%d\n",sum);
101     }
102     return 0;
103 }

 

目录
相关文章
|
存储 算法 C++
C++基础算法离散化及区间合并篇
C++基础算法离散化及区间合并篇
185 0
|
8月前
技术心得:曲率计算公式推导
技术心得:曲率计算公式推导
139 0
|
人工智能 算法
算法提高:组合数学| 容斥原理常见应用
容斥原理常见的问题如下。 (1) 篮球、羽毛球、网球三种运动,至少会一种的有22人,会篮球的有15人,会羽毛球的有17人,会网球的有12人,既会篮球又会羽毛球的有11人,既会羽毛球又会网球的有7人,既会篮球又会网球的有9人,那么三种运动都会的有多少人? (2) 《西游记》《三国演义》《红楼梦》三大名著,至少读过其中一本的有20人,读过《西游记》的有10人,读过《三国演义》的有12人,读过《红楼梦》的有15人,读过《西游记》《三国演义》的有8人,读过《三国演义》《红楼梦》的有9人,读过《西游记》《红楼梦》的有7人。问三本书全都读过的有多少人?
205 0
算法提高:组合数学| 容斥原理常见应用
|
9月前
|
算法
R语言贝叶斯METROPOLIS-HASTINGS GIBBS 吉布斯采样器估计变点指数分布分析泊松过程车站等待时间
R语言贝叶斯METROPOLIS-HASTINGS GIBBS 吉布斯采样器估计变点指数分布分析泊松过程车站等待时间
|
机器学习/深度学习 算法
算法提高:组合数学| 卡特兰数的实现
卡特兰数列是组合数学中在各种计数问题中常出现的数列,其前几项为1,1,2,5,14,42,132,429,1430,4862,16796,58786,208012…… 卡特兰数首先是由欧拉在计算对凸n边形的不同的对角三角形剖分的个数问题时得到的,即在一个凸n边形中,通过不相交于n边形内部的对角线,把n边形拆分成若干三角形,不同的拆分数用Hn表示,Hn即卡特兰数。
164 0
算法提高:组合数学| 卡特兰数的实现
|
存储 算法
PDE优化|逆问题中偏微分方程约束优化的惩罚方法(Matlab代码实现)
PDE优化|逆问题中偏微分方程约束优化的惩罚方法(Matlab代码实现)
245 0
|
移动开发 算法
一、基础算法(快排,归并,二分,高精度,前缀和,差分)
基础算法(快排,归并,二分,高精度,前缀和,差分)
77 0
贪心——376. 摆动序列
本专栏按照数组—链表—哈希—字符串—栈与队列—二叉树—回溯—贪心—动态规划—单调栈的顺序刷题,采用代码随想录所给的刷题顺序,一个正确的刷题顺序对算法学习是非常重要的,希望对大家有帮助
贪心——376. 摆动序列
LDUOJ——山(计算几何+二分+精度)
LDUOJ——山(计算几何+二分+精度)
99 0