经典贪心(区间覆盖)

Problem E
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 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


每个圆形有效的覆盖面积可以转化成一个长方形,今儿就可以转化成区间覆盖的问题

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
int N;
double L,W;
struct node
{
    double x,y;
    bool operator<(const node &b)const
    {
        return x<b.x;
    }
}a[maxn];
int main()
{
    while(scanf("%d%lf%lf",&N,&L,&W)!=EOF)
    {
        int cnt=0;
        for(int i=1;i<=N;i++)
        {
            double x,r;
            scanf("%lf%lf",&x,&r);
            if(2*r<=W)continue;
            double tmp=sqrt(r*r-W*W/4);
            a[++cnt].x=x-tmp;
            a[cnt].y=x+tmp;
        }
        sort(a+1,a+cnt+1);
        int i=1,ans=0,flag=0;
        if(a[1].x>0){printf("-1\n");continue;}
        double left=0,right=0;
        while(i<=cnt)
        {
            int j=i;
            while(j<=cnt&&left>=a[j].x)
            {
                if(a[j].y>right)
                    right=a[j].y;
                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");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值