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 }

 

目录
相关文章
|
6月前
|
Java
hdu1181 变形课(暴力搜索法)
hdu1181 变形课(暴力搜索法)
28 0
|
9月前
|
存储 算法 调度
【喜闻乐见,包教包会】二分图最大匹配:匈牙利算法(洛谷P3386)
【喜闻乐见,包教包会】二分图最大匹配:匈牙利算法(洛谷P3386)
48 0
|
12月前
|
BI
【AcWing】差分及其应用
所谓差分,就是前缀和的逆运算
53 0
|
机器学习/深度学习 算法
模拟退火-n皇后问题
模拟退火-n皇后问题
HDU7018.Banzhuan(计算几何+贪心)
HDU7018.Banzhuan(计算几何+贪心)
81 0
HDU7018.Banzhuan(计算几何+贪心)
|
人工智能
Codeforces1491——C.Pekora and Trampoline(差分思维+树状数组)
Codeforces1491——C.Pekora and Trampoline(差分思维+树状数组)
86 0
Codeforces1491——C.Pekora and Trampoline(差分思维+树状数组)
洛谷P1067-多项式输出(模拟好题!)
洛谷P1067-多项式输出(模拟好题!)
|
开发者
膜拜(离散化差分模板题)
题目描述 小鱼有 n 名优秀的粉丝。 粉丝们得知小鱼将会在一条直线上出现,打算去膜他。为了方便,粉丝们在这条直线上建立数轴。 第 i 名粉丝有一个侦查区间[li,ri] 。如果小鱼在 j(li≤j≤ri) 处出现,这名粉丝将立刻发现并膜他。 小鱼希望膜他的人越多越好,但是他不能分身,因此只能选择一个位置出现。 小鱼想知道自己最多能被多少个人膜。
186 0
膜拜(离散化差分模板题)
|
存储
【洛谷】【动态规划】【高精度】P1018 [NOIP2000 提高组] 乘积最大
【洛谷】【动态规划】【高精度】P1018 [NOIP2000 提高组] 乘积最大
218 0