Watering Grass UVA - 10382 (几何图形 贪心)

题意:给定一个L*W的矩形和若干圆,求最少用多少圆能够将矩形区域全部覆盖。若无解输出-1,否则输出最小的数目。

思路:贪心思路,每次设定当前已覆盖的区域最右侧边界x,每次找l<=x,r>=x中r的最大值,计数加一,x更新为r。写的时候一直出错是一直当圆形看,其实最简单的方法就是求出每个圆占据的L*W大矩形区域的小矩形面积l*W,将n个圆转化成n个矩形。(若圆的直径小于等于W,怎该圆无法占据矩形区域,忽略此圆即可)

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<cstdlib>
#include<sstream>
#include<deque>
#include<stack>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-9;
const int  maxn = 10000 + 20;
const int  maxt = 300 + 10;
const int mod = 10;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const int Dis[] = {-1, 1, -5, 5};
const double inf = 0x3f3f3f3f;
const int MOD = 1000;
int n, m, k;
double L, W;
struct node{
    double l;
    double r;
    bool operator < (const node &no) const{
        if(l != no.l) return l < no.l;
        return r < no.r;
    }
}num[maxn];
int main(){
    while(~scanf("%d%lf%lf", &n, &L, &W)){
        int cnt = -1;
        double p, r, x;
        for(int i = 0; i < n; ++i){
            scanf("%lf%lf", &p, &r);
            if(2 * r <= W) continue;
            x = sqrt(r * r - (W / 2.0) * (W / 2.0));
            num[++cnt].l = p - x;//计算每个圆在大矩形中占得小矩形的左右边界
            num[cnt].r = p + x;
        }
        sort(num, num + cnt + 1);
        if(cnt == -1 || num[0].l > 0){
            printf("-1\n"); continue;
        }
        x = 0;//x表示当前大矩形中已经覆盖好的矩形的最右侧边界
        bool ok = true;
        int ans = 0;
        int cur = 0;
        while(cur <= cnt){
            int tmpi = -1;
            double tmpm = -1;
            for(int i = cur; i <= cnt; ++i){
                if(num[i].l <= x && num[i].r >= x){
                    if(num[i].r > tmpm){
                        tmpm = num[i].r;//记录当前最右侧边界
                        tmpi = i;//对应第几个圆(小矩形)
                    }
                }
            }
            if(tmpm == -1){
                ok = false; break;
            }
            ++ans;
            x = tmpm;//更新最右侧边界
            cur = tmpi + 1;
            if(x >= L) break;
        }
        if(x >= L) printf("%d\n", ans);
        else printf("-1\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值