Watering Grass

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

题意:给定一条草坪,草坪上有n个喷水装置,草坪长l米宽w米,n个装置都有每个装置的位置和喷水半径,要求出最少需要几个喷水装置才能喷满草坪,喷水装置都是装在草坪中间一条水平线上的。

在这里插入图片描述
根据这图可以看出,一个喷水装置的有效覆盖范围就是圆中间的那个矩形。所以,在输入的同时,进行预处理,转换成矩形左边的坐标和右边的坐标。 这样,其实就转换成了经典的区间覆盖问题。

这题目的大致思路如下:
(1)如果圆的半径r*2<=草坪的宽度w,直接忽略该圆。
(2)对所有要考虑的圆按该圆与草坪上边界的的左交点的x坐标从小到大排序。
(3)如果所有圆能覆盖整个草坪,等价于圆与草坪的上边界交点所形成的区间覆盖了整个长度l。
(4)排完序后的圆,如果第一个的左交点x大于0,则无解。
(5)贪心选择圆的左交点不大于某个参考值,而右交点却最大的那个。(这里的参考值,一开始为0,如果找到了右交点最大的那个圆,就更新它)另外,如果找不到这样的圆,则无解。

( 6)区间覆盖问题, 这里要注意一下每个喷头的有效作用区间并不是[p - r, p+ r], 因为圆是有弧度的,所以有效区间为t = sqrt(r * r - h * h /4)(根据勾股定理求得), 这里要先判断r 和 h/2 的大小, [ p - t, p + t].(见上图)

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
struct node
{
    double z,y;
}a[10010];
int cmp(node a,node b)
{
    return a.z<b.z;
}
int sign[10010];
int main()
{
    int n,l,i,j,w,k;
    int p,r;
    while(~scanf("%d%d%d",&n,&l,&w))
    {

        int ss=0;
        for(i=0; i<n; i++)
        {
            scanf("%d%d",&p,&r);
            if(r<=w/2.0) continue;
            double tmp=sqrt((double)r*r-(double)w*w/4.0);//注意这里是double型

            a[ss].z=p-tmp;//所能覆盖的最左端
            a[ss].y=p+tmp;//所能覆盖的最右端
            ss++;
        }
        sort(a,a+ss,cmp);

        if(a[0].z>0)
        {
            printf("-1\n");
            continue;
        }
        int flag=0,s=0;
        double cnt=0,cen=0;
        i=0;
        while(i<ss)//当还未完全覆盖时继续循环
        {
            j=i;
            while(j<ss&&cnt>=a[j].z)
            {
                if(cen<a[j].y)// 满足衔接条件,则更新cen点
                    cen=a[j].y;
                j++;
            }
            if(i==j) break;
            s++;
            cnt=cen;
            i=j;
            if(cnt>=l)
            {
                flag=1;
                break;
            }
        }
        if(flag)
            printf("%d\n",s);
        else
            printf("-1\n");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值