UVA10382 - Watering

Problem E
Watering Grass
Input: standard input
Output: standard output
Time Limit: 3 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 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


(Regionals 2002 Warm-up Contest, Problem setter: Piotr Rudnicku)

题目大意:有一块草坪,长l,宽w,在草坪的中心线上的不同位置安装有n个喷水装置。喷水装置i可以将以它为中心,半径为ri的圆形区域湿润。要求选择尽量少的喷水装置,把草坪全部湿润。

题解:通过观察样例1的图片发现,喷水装置的有效湿润区域是圆形区域和矩形公共部分,而实际湿润区域是圆形区域和矩形产生的矩形,即下图(此图来源于shuangde800):

                                             UVA10382 - Watering

 

这样我们就把圆转换成矩形了,对于半径小于矩形宽度的可以过滤掉。题目也就可以用区间覆盖来解决了,这样就和UVA10020 - Minimal coverage如出一辙了。不过很诡异的是如果输入的喷水装置的位置和半径是int类型,提交上去会TLE,改成double就AC了。。。。

View Code
 1 #include<stdio.h>

 2 #include<string.h>

 3 #include<math.h>

 4 #define MAXSN 10005

 5 typedef struct

 6 {

 7     double l;

 8     double r;

 9 } NODE;

10 NODE a[MAXSN];

11 void qsort(int l,int r)

12 {

13     int i,j;

14     double mid;

15     NODE temp;

16     i=l;

17     j=r;

18     mid=a[(l+r)>>1].l;

19     while(i<=j)

20     {

21         while(a[i].l<mid) i++;

22         while(a[j].l>mid) j--;

23         if(i<=j)

24         {

25             temp=a[i];

26             a[i]=a[j];

27             a[j]=temp;

28             i++;

29             j--;

30         }

31     }

32     if(i<r) qsort(i,r);

33     if(j>l) qsort(l,j);

34 }

35 int main(void)

36 {

37     int n,l,i,ans,w;

38     double rr,maxr,p,r;

39     while(scanf("%d%d%d",&n,&l,&w)!=EOF)

40     {

41         int m;

42         m=0;

43         for(i=0; i<n; i++)

44         {

45 

46             scanf("%lf%lf",&p,&r);

47             if(r*2<=w) continue;

48             a[m].l=p-(sqrt(r*r-w*w/4.0));

49             a[m].r=p+(sqrt(r*r-w*w/4.0));

50             m++;

51         }

52         qsort(0,m-1);

53         ans=0;

54         if(a[0].l>0)

55         {

56             printf("-1\n");

57             continue;

58         }

59         rr=0;

60         ans=0;

61         i=0;

62         maxr=-1;

63         while(i<m)

64         {

65             while(a[i].l<=rr&&i<m)

66             {

67                 if(a[i].r>maxr) maxr=a[i].r;

68                 i++;

69             }

70             rr=maxr;

71             ans++;

72             if(rr>l) break;

73             if(a[i].l>rr) break;

74         }

75         if(rr<l)

76             printf("-1\n");

77         else

78             printf("%d\n",ans);

79 

80     }

81     return 0;

82 }

 

 

你可能感兴趣的:(water)