浏览器标题切换
浏览器标题切换end

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

 

题意:给你n个喷水装置,给出其长度和宽度,接下去给出其中心和半径,问最少需要多少个喷水装置能够覆盖整条草条。

思路:利用勾股定理求出每个装置能够到达的左区间端点和右区间端点并存在结构体中,进行排序(左区间从小到大,右区间从大到小),最后转化成区间覆盖问题即可。

特判:

  1. 排序完成后的第一个区间的左端点需要小于等于0,最大的右端点需要大于等于草条的长度
  2. 需要考虑下一个区间的左端点小于等于当前区间的右端点,但是其右端点也小于等于该点的右区间
  3. 利用flag进行标记,若在遍历的过程中出现中间断开,也就是说下一个区间的左端点比当前的最大右区间还要大

考虑到7-15、13-17、15-20的这种情况,很容易把第二个算进去,本来通过变量去记录最长距离,然后去更新变量的距离和坐标,通过for循环结束,但是发现这样做右可能在测试数据上过不去,而随时随地能够终止循环的需要利用while循环来写。

已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

 

 

posted @ 2019-07-21 07:00  抓水母的派大星  阅读(173)  评论(0编辑  收藏  举报