nyist 12 喷水装置(二)



喷水装置(二)

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 4
描述
有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水装置,每个喷水装置i喷水的效果是让以它为中心半径为Ri的圆都被润湿。请在给出的喷水装置中选择尽量少的喷水装置,把整个草坪全部润湿。
输入
第一行输入一个正整数N表示共有n次测试数据。
每一组测试数据的第一行有三个整数n,w,h,n表示共有n个喷水装置,w表示草坪的横向长度,h表示草坪的纵向长度。
随后的n行,都有两个整数xi和ri,xi表示第i个喷水装置的的横坐标(最左边为0),ri表示该喷水装置能覆盖的圆的半径。
输出
每组测试数据输出一个正整数,表示共需要多少个喷水装置,每个输出单独占一行。
如果不存在一种能够把整个草坪湿润的方案,请输出0。
样例输入
2
2 8 6
1 1
4 5
2 10 6
4 5
6 5
样例输出
1
2
装换为贪心区间覆盖问题 左端点从小到大排序 每次选区间最长的,
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
struct xxx
{
    double a,b;
} k[10010];
bool  cmp(xxx q1,xxx q2)
{
    return q1.a<q2.a;
}
int main()
{
    int t,n,i,cou;
    double w,h,xi,ri;
    cin>>t;
    while(t--)
    {
        cin>>n>>w>>h;
        cou=0;
        for(i=0; i<n; i++)
        {
            cin>>xi>>ri;
            if(ri<=h/2)
                continue;
            double m=sqrt(ri*ri-h*h/4);
           // cout<<" m="<<m<<endl;
            k[cou].a=xi-m;
            k[cou].b=xi+m;
            cou++;
        }
        sort(k,k+cou,cmp);
        int ans=0,ok=1;
        double MAX=0,sum=0;
       // cout<<k[0].a<<endl;
        while(sum<w)
        {
            MAX=0;
            for(i=0; i<cou&&k[i].a<=sum; i++)
            {
                //cout<<"i="<<i<<endl;
               // cout<<k[i].b-sum<<endl;
                if(k[i].b-sum>MAX)
                {
                    MAX=k[i].b-sum;
                }
            }
          //  cout<<"MAX="<<MAX<<endl;
            if(MAX==0)
            {
                ok=0;
                break;
            }
            else
            {
                sum=sum+MAX;
                ans++;
            }
        }
        if(ok==0)
            cout<<"0"<<endl;
        else
            cout<<ans<<endl;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值