喷水装置(二)(最小线段覆盖)

本文介绍了一个关于选择最少数量的喷水装置来完全润湿一块指定大小草坪的问题,并提供了一种有效的贪心算法解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目地址

题目大意:有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水装置,每个喷水装置i喷水的效果是让以它为中心半径为Ri的圆都被润湿。请在给出的喷水装置中选择尽量少的喷水装置,把整个草坪全部润湿,题目给出每个装置的横坐标和半径。

解题思路:先将没用的装置(半径<h/2)去掉,再用贪心选择最合适的

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <stack>

using namespace std;

const int maxn = 1e4+100;

struct Circle
{
    double l,r;
}cir[maxn];

bool cmp(Circle a,Circle b)
{
    if(a.l == b.l)  return a.r> b.r;
    return a.l < b.l;
}

int main()
{
    int t,n,cnt;
    double x,y,w,h;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%lf%lf",&n,&w,&h);
        double minn = 0xffffff;
        double maxx = 0;
        cnt = 0;
        for(int i = 0; i < n; i++)
        {
            scanf("%lf%lf",&x,&y);
            if(y>=h/2)
            {
                cir[cnt].l = x-sqrt(y*y-h*h/4);
                cir[cnt].r = x+sqrt(y*y-h*h/4);
                minn = min(minn,cir[cnt].l);
                maxx = max(maxx,cir[cnt].r);
                cnt++;
            }
        }
        if(minn > 0 || maxx < w)
        {
            puts("0");
            continue;
        }
        sort(cir,cir+cnt,cmp);
        double l=0,r=0;
        int flag = 0;
        maxx = 0;
        int ans = 0;
        while(r < w)
        {
            l = cir[flag].l;
            r = cir[flag].r;
            for(int i = 0; i < cnt; i++)
            {
                if(cir[i].l <= maxx && cir[i].r > r)
                {
                    l = cir[i].l;
                    r = cir[i].r;
                    flag = i;
                }
            }
            flag++;
            maxx = max(maxx,r);
            ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}



### 关于喷水装置的相关信息 #### 草坪浇灌问题描述 对于 \( L \) 米,宽 \( W \) 米的草坪来说,在其中安装了 \( n \) 个浇灌喷头。每个喷头位于草坪中心线上(距离两边各 \( \frac{W}{2} \)),并且已知其位置(即该喷头距草坪左端的距离)及其能够覆盖的最大半径范围[^3]。 为了简化计算过程,通常会将圆形喷洒区域转换为线段理。具体而言,设某喷头的位置为 \( p \),最大射程为 \( r \),则对应的线段起点和终点分别为: \[ a[cnt].s = p - \sqrt{(r^2)-\left(\frac{w}{2}\right)^2}; \] 这样做的目的是因为实际有效的灌溉面积仅限于圆心部分而非整个圆形区域[^1]。 #### 解决方案概述 目标是在满足条件的情况下尽可能少地开启喷头数量以完成全部草坪的浇水工作。当所有可用喷头都无法完全覆盖整片草地时,则返回 `-1` 表明无法实现全面灌溉[^4]。 针对此类区间选择类题目,常用的方法之一就是采用贪心算法策略:按照各个喷头所能触及最远右边界进行排序;随后遍历这些经过排序后的候选者们,并总是优先选取当前可选范围内延伸得最远的那个作为下一个使用的喷头直到结束或者发现无解情况为止[^2]。 ```cpp #include <iostream> #include <cmath> #include <algorithm> using namespace std; struct Sprinkler { double start, end; }; bool cmp(Sprinkler s1, Sprinkler s2){ return s1.end < s2.end; } int main(){ int T; cin >> T; while(T--){ int l,w,n,cnt=0; scanf("%d%d%d",&l,&w,&n); struct Sprinkler sprinklers[n]; for(int i=0;i<n;++i){ double pos,radius; scanf("%lf %lf", &pos, &radius); // Convert circle to line segment based on given formula. sprinklers[i].start = max(0., pos-sqrt(pow(radius,2)-(pow(w/2.0,2)))); sprinklers[i].end = min(l*1.0 ,pos+sqrt(pow(radius,2)-(pow(w/2.0,2)))); } sort(sprinklers,sprinklers+n,cmp); double curPos = 0.; bool canCoverAll=true; int usedSprinklersCount=0; for (auto& spr : sprinklers) { if(curPos >= l || !canCoverAll){break;} if(canCoverAll && spr.start<=curPos){ ++usedSprinklersCount; curPos=spr.end; continue; }else{ canCoverAll=false; } } cout << ((curPos>=l)?usedSprinklersCount:-1)<<endl; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值