Watering Grass UVA - 10382(贪心&&模拟)区间覆盖

这篇博客讨论了一种将喷水装置抽象为线段并转换为区间覆盖问题的方法,以解决在长方形区域内用最少的喷水装置完全覆盖的问题。作者指出需要注意半径不足的情况,并提供了一个AC的C++代码实现,该代码处理了浮点数除法和边界条件,最终求得最少装置数量。
摘要由CSDN通过智能技术生成

Watering Grass UVA - 10382

题意:

给出每个喷水装置的位置,和喷水半径。
给你一个长方形区域,要求你装最少的喷水装置,使得这个区域都可以被喷到水。

思路:

  1. 首先可以想到先把喷水装置 抽象成线段。(但是这一步要小心一下,每个喷水点在竖轴的中心)
  2. 之后把问题转换为了区间覆盖。

反思

  1. 本题要考虑假如半径不够 w 2 \frac{w}{2} 2w的情况,那么肯定不采用这个装置。qwq。。。。
  2. 还要考虑,假如更新到了超过长度 l l l,那么可以不用更新了,直接break出循环。
  3. 一开始没考虑浮点数除法。。。(在sqrt那里。。。),后面直接全换double。

AC

#include <iostream>
#include <bits/stdc++.h>
#define For(i,x,y) for(int i=(x); i<=(y); i++)
#define fori(i,x,y) for(int i=(x); i<(y); i++)
#define rep(i,y,x) for(int i=(y); i>=(x); i--)
#define mst(x,a) memset(x,a,sizeof(x))
#define pb push_back
#define sz(a) (int)a.size()
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<int,int>pa;
typedef pair<ll,ll>pai;
const int maxn = 1e4+10;
const double eps = 1e-6;
struct QU{
    double l, r;
} qu[maxn];
int main()
{
    //ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n;
    double len, w;
    while(~scanf("%d%lf%lf", &n, &len, &w)){
        double p, r;
        int cnt = 0;
        fori(i,0,n){
            scanf("%lf%lf", &p, &r);
            double len = sqrt(r*r-w*w/4);///
            if(r < w/2)continue;
            qu[cnt++]={p-len,p+len};
        }
        sort(qu,qu+cnt,[&](QU x, QU y){return x.l < y.l;});
        int ans = 0;
        double now = 0;
        for(int i=0; i<cnt; i++){
            double mx = 0;
            for(int j=i; j<cnt && qu[j].l <= now; j++){
                //if(mx<qu[j].r)mx = qu[j].r;
                mx = max(mx,qu[j].r);
                i = j;
            }
            if(mx > now){
                now = mx;
                ans++;
            }else break;
            if(now >= len)break;///
        }
        if(now >= len)printf("%d\n", ans);
        else printf("-1\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值