喷水装置---Watering Grass

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 andw 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 numbersn, 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
思路分析:

读入数据,并计算其左右端点:

左:x-sqrt(r*r-(w/2)*(w/2));

右:x-sqrt(r*r+(w/2)*(w/2));

2.对其左端点进行从大到小排序

3.从左到右依次处理区间,注意要考虑的情况:

(1)若最小左端点不满足,直接得出不可以完全覆盖;

(2)若中间存在间断,得不可以完全覆盖;

(3)若到最后也不能满足右端点达到草坪的长度,得不可以完全覆盖。

代码如下:

  1. #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    int n,l,w;
    struct node
    {
        double x,xx,yy;
    } a[120000];
    bool cmp(node u,node v)
    {
        return u.xx<v.xx;
    }
    int book[12000];
    int main()
    {
        while(~scanf("%d %d %d",&n,&l,&w))
        {
            double b,c;
            int j=0;
            for(int i=0; i<n; i++)
            {
                scanf("%lf %lf",&b,&c);
                if(c*2>w)        //舍去无效的喷水装置,并求有效的喷水装置的左右端点
                {
                    a[j].x=b;
                    double r=sqrt(c*c-w*w*1.0/4);
                    a[j].xx=b-r;    //左端点
                    a[j++].yy=b+r;  //右端点
                }
            }
            sort(a,a+j,cmp);        //对左端点进行排序
            if(a[0].xx>0)           //如果最小的左端点小于0则不可能完全覆盖
            {
                printf("-1\n");     
                continue;
            }
            double k=0,h=0;
            int sum=0,flag,q;
            memset(book,0,sizeof(book)); 
            while(1)
            {
                flag=0;
                for(int i=0; i<j; i++)        
                {
                    if(a[i].xx<=h&&a[i].yy>=k&&book[i]==0)//遍历,去更新满足左端点的最大右端点
                    {
                        flag=1;
                        k=a[i].yy;
                        if(k>=l)
                            flag=2;
                        q=i;
                    }
                }
                if(flag==1||flag==2)
                {
                    sum++;
                    h=k;
                    book[q]=1;    //对于用过的点进行标记
                }
                else
                    break;        //找不到满足的情况直接跳出循环,证明不能完全覆盖
                if(flag==2)       //已经满足完全覆盖,跳出循环
                    break;    
            }
            if(k<l)
                flag=0;
            if(flag==0)
                printf("-1\n");
            else
                printf("%d\n",sum);
        }
        return 0;
    }

     

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值