UVA 10382 Watering Grass greedy + interval coverage problems

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’.

 

Question is intended: to give you the n sprinkler, given its length and width, the center and radius are given next, a minimum number of sprinklers Q needs to be able to cover the entire swath.

Ideas: the use of the left and right segment endpoints determined interval endpoints Pythagorean theorem and each device can be reached in the present structure, sort (left range from small to large, the right descending interval), and finally converted to the interval coverage problems It can be.

Tokuban:

  1. A left end of the first section after the completion of the sort required less than or equal to 0, the right end point must be greater than the maximum swath equal to the length of
  2. The next point to consider the left section less than or equal right point of the current segment, but also the right end point of less than or equal to the right section
  3. Use flag marked, if there mid off, that left the next interval point during traversal bigger than the current maximum range of the right

7-15,13-17,15-20 view of this, it is easy to second enumerated, have been recorded by the longest distance to the variables, and then to update the distance coordinate variables, for loop end, but found that the right to do so may make life difficult on the test data, and can be terminated at any time cycle need to use a while loop to write.

已 AC:

  1 #include<iostream>
  2 #include<math.h>
  3 #include<stdio.h>
  4 #include<algorithm>
  5 #define inf 0x3f3f3f3f
  6 using namespace std;
  7 
  8 struct node
  9 {
 10     double l;
 11     double r;
 12 } a[10020];
 13 
 14 int cmp1(node x,node y)
 15 {
 16     if(x.l!=y.l)
 17         return x.l<y.l;
 18     else
 19         return x.r>y.r;
 20 
 21 }
 22 //按左边界的坐标点从小到大排序,//右端点为什么不需要排序
 23 
 24 
 25 int main()
 26 {
 27     std::ios::sync_with_stdio(false);
 28     int n;
 29     double len,w;
 30     double c,rr;
 31     while(cin>>n>>len>>w)
 32     {
 33         int num=0;
 34         double maxxr=-1.0;
 35         for(int i=0; i<n; i++)
 36         {
 37             cin>>c>>rr;
 38             if(rr*2<=w)
 39             {
 40                 continue;
 41             }
 42             else
 43             {
 44                 double l=c-sqrt(rr*rr-w*w/4);
 45                 double r=c+sqrt(rr*rr-w*w/4);
 46               //   printf("%lf--%lf\n",l,r);
 47                 if(r>=maxxr)
 48                 {
 49                     maxxr=max(maxxr,r);
 50                 }
 51                 //      if(l<=0)
 52                 //   l=0;
 53                 a[num].l=l;
 54                 a[num++].r=r;
 55             }
 56         }
 57         sort(a,a+num,cmp1);
 58         int k=0;
 59         if(a[0].l>0||maxxr<len)
 60         {
 61             cout<<-1<<endl;
 62             continue;
 63         }
 64         double maxx=0;
 65         int ans=0;
 66         int flag=0;
 67         int ww=0;
 68         while(maxx<len)
 69         {
 70             double uu=maxx;
 71             for(int i=0; i<num; i++)
 72             {
 73                 if(a[i].l<=uu&&a[i].r>maxx)
 74                 {
 75                    // minn=a[i].l;
 76                     maxx=a[i].r;
 77 //                    zz=i;
 78                 }
 79             }
 80            // printf("%lf----%d\n",maxx,zz);
 81            if(uu==maxx&&uu<len)
 82            {
 83                ww=1;break;
 84            }
 85             //minn=a[zz].l;
 86             ans++;
 87             /*for(int i=0; i<num; i++)
 88             {
 89                 //printf("%lf*****%lf\n",a[i].l,a[i].r);
 90                 if(a[i].l<=maxx)
 91                 {
 92                     if(a[i].r>maxx)
 93                     {
 94                         maxx=a[i].r;
 95                         ans++;
 96                         printf("%lf----%lf\n",a[i].l,a[i].r);
 97                     }
 98                     else
 99                     {
100                         continue;
101                     }
102                 }
103                 if(a[i].l>maxx)
104                 {
105                     flag=0;
106                     break;
107                 }
108                 if(a[i].r>=len)
109                 {
110                     flag=1;
111                     break;
112                 }*/
113             //}//中间部分要是连接不上,处理:进行flag标记
114         }
115        // printf("%d\n",ans);
116         if(ww==0)
117             cout<<ans<<endl;
118         else
119             cout<<-1<<endl;
120     }
121     return 0;
122 }
View Code

 

利用for循环的代码,代码是错误的,但有这个思路在

  1 #include<iostream>
  2 #include<math.h>
  3 #include<stdio.h>
  4 #include<algorithm>
  5 #define inf 0x3f3f3f3f
  6 using namespace std;
  7 
  8 struct node
  9 {
 10     double l;
 11     double r;
 12 } a[10020];
 13 
 14 int cmp1(node x,node y)
 15 {
 16     if(x.l!=y.l)
 17         return x.l<y.l;
 18     else
 19         return x.r>y.r;
 20 
 21 }
 22 //按左边界的坐标点从小到大排序
 23 
 24 
 25 int main()
 26 {
 27     //std::ios::sync_with_stdio(false);
 28     int n;
 29     double len,w;
 30     double c,rr;
 31     while(cin>>n>>len>>w)
 32     {
 33         int num=0;
 34         double maxxr=-1.0;
 35         for(int i=0; i<n; i++)
 36         {
 37             cin>>c>>rr;
 38             if(rr*2<=w)
 39             {
 40                 continue;
 41             }
 42             else
 43             {
 44                 double l=c-sqrt(rr*rr-w*w/4);
 45                 double r=c+sqrt(rr*rr-w*w/4);
 46             //    printf("%lf--%lf\n",l,r);
 47                 if(r>=maxxr)
 48                 {
 49                     maxxr=max(maxxr,r);
 50                 }
 51                 //      if(l<=0)
 52                 //   l=0;
 53                 a[num].l=l;
 54                 a[num++].r=r;
 55             }
 56         }
 57         sort(a,a+num,cmp1);
 58         int k=0;
 59         int pp=0;
 60         if(a[0].l>0||maxxr<len)
 61         {
 62             cout<<-1<<endl;
 63             continue;
 64         }
 65         double maxx=0;
 66         double minn=0;
 67         int ans=0;
 68         int flag=0;
 69         int w=0;
 70         int ww=0;
 71         int d=0;
 72         int z=0;
 73         int qq=0;
 74         for(int i=0; i<num; i=qq)
 75         {
 76             //printf("%lf*****%lf\n",a[i].l,a[i].r);
 77             if(a[i].l<=maxx)
 78             {
 79                 if(a[i].r>maxx)
 80                 {
 81                     if(maxx>=len)
 82                     {
 83                         printf("%d\n",ans);
 84                         pp=1;
 85                     }
 86                     w=i;
 87                     ww=a[i].r-a[i].l;
 88                  //   maxx=a[i].r;
 89                  //     ans++;
 90                     //   printf("%lf----%lf\n",a[i].l,a[i].r);
 91                     for(int j=i+1;j<num;j++)
 92                     {
 93                         z=a[j].r-a[j].l;
 94                         if(a[j].l<=maxx&&a[j].r>maxx)
 95                         {
 96                             if(z>ww)
 97                             {
 98                                 w=z;
 99                             }
100                         }
101                         if(a[j].l>maxx)
102                             break;
103                     }
104              //       printf("%lf----%lf\n",a[i].l,a[i].r);
105                     maxx=a[w].r;
106                     qq=w;
107                     printf("%lf\n",maxx);
108                     w=0;
109                     ans++;
110                     if(maxx>=len)
111                     {
112                         printf("%d\n",ans);
113                         pp=1;
114                     }
115                     printf("%d\n",ans);
116                    if(pp==1)
117                     break;
118                 }
119                 else
120                 {
121                     continue;
122                 }
123                 if(pp==1)
124                     break;
125             }
126 
127 //            if(a[i].l>maxx)
128 //            {
129 //                flag=0;
130 //                break;
131 //            }
132 //            if(a[i].r>=len)
133 //            {
134 //                flag=1;
135 //                break;
136 //            }
137         }
138         // printf("%d\n",ans);
139 
140 
141       //  if(flag)
142       /*if(maxx>=len)
143             cout<<ans<<endl;
144      */
145         if(pp==0)
146             cout<<-1<<endl;
147     }
148     return 0;
149 }
View Code

 

 

Guess you like

Origin www.cnblogs.com/OFSHK/p/11220101.html
Recommended