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’.
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
又是一个区间覆盖的问题,和实验室前几周的问题大体上一样。这里只不过是圆形了。我们可以将这个圆形所覆盖的左最小和右最大储存在结构体里。和https://blog.csdn.net/starlet_kiss/article/details/84591730这个题一样的意思。然后我按着这个题目去解,但是wa了好几发。无奈之下,我去搜题解,发现题解里的是按着最小左覆盖去排序的,而我的是按着最大右覆盖去排序。按着题解写了一下,提交了然后过了。我又去比较两种方法的不同。发现我落下一个条件,就是当圆的半径小于等于矩形的宽度时,是没法浇倒水的。我把这个条件加上,然后就过了,,,阿西吧。
两个代码都贴一下吧。
代码如下:
最小左覆盖
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n;
const int maxx=1e4+10;
double l,w;
double v,r;
struct node{
double l;
double r;
}p[maxx];
int cmp(const node &a,const node &b)
{
return a.l<b.l;
}
int main()
{
while(scanf("%d%lf%lf",&n,&l,&w)!=EOF)
{
int cnt=0;
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&v,&r);
if(w/2>=r) continue;
p[cnt].l=v-sqrt(r*r-w*w/4.0);
p[cnt++].r=v+sqrt(r*r-w*w/4.0);
}
sort(p,p+cnt,cmp);
int ans=0;
double left=0,right=0;
int flag=0;
if(p[0].l<=0)
{
int i=0;
while(i<cnt)
{
int j=i;
while(j<cnt&&left>=p[j].l)
{
if(p[j].r>right)
right=p[j].r;
++j;
}
if(j==i) break;
++ans;
left=right;
i=j;
if(left>=l)
{
flag=1;
break;
}
}
}
if(flag) printf("%d\n",ans);
else printf("-1\n");
}
}
最大右覆盖
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxx=1e4+10;
struct node{
double l,r;
}p[maxx];
int n;
double l,w;
double v,r;
int cmp(const node &a,const node &b)
{
return a.r>b.r;
}
int main()
{
while(scanf("%d%lf%lf",&n,&l,&w)!=EOF)
{
int cnt=0;
for(int i=0;i<n;i++)
{
scanf("%lf%lf",&v,&r);
if(w/2>=r) continue;//!!!!不要忘了
p[cnt].l=v-sqrt(r*r-w*w/4);
p[cnt++].r=v+sqrt(r*r-w*w/4);
}
sort(p,p+cnt,cmp);
double ant=0;
int ans=0;
int i,j;
while(ant<l)
{
for(i=0;i<cnt;i++)
{
if(p[i].l<=ant&&p[i].r>ant)
{
ant=p[i].r;
ans++;
break;
}
}
if(i==cnt) break;
}
if(ant<l) printf("-1\n");
else printf("%d\n",ans);
}
}
努力加油a啊,(o)/~