UVA - 10382 Watering Grass 贪心(几何)

几何专场中的一道题目

其实更是贪心


更定每一个圆后计算左右能覆盖的边界

然后贪心处理

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <ctype.h>
#include <vector>
#include <algorithm>
#include <sstream>
#define PI acos(-1.0)
// cout << "  ===  " << endl;

using namespace std;
typedef long long ll;
const int maxn = 10000 + 7, INF = 0x3f3f3f3f, mod = 1e9+7;
int n, L, h;
double c;
struct node {
    double o, r;
    double l_, r_;
}a[maxn];
bool cmp(node a, node b) {
    if(a.l_ == b.l_) return a.r_ < b.r_;
    return a.l_ < b.l_;
}

void init() {
    for(int i = 0; i < n; ++i) {
        scanf("%lf%lf", &a[i].o, &a[i].r);
        if(a[i].r <= c) {
            a[i].l_ = a[i].r_ = 0;
        }
        else {
            double t = sqrt((a[i].r)*(a[i].r) - (c)*(c));
            a[i].l_ = a[i].o - t;
            a[i].r_ = a[i].o + t;
        }
    }
    sort(a, a+n, cmp);
}

void solve() {
    double ansl = 0.0, ansr = 0.0;

    if(a[0].l_ > ansl) { cout << -1 << endl; return; }
    int i = 0, cnt = 0;
    while(1) { 
        while(a[i].l_ <= ansl) {
            ansr = max(ansr, a[i].r_);
            i++;
            if(i >= n) break;
        }
        cnt++;
        if(ansr >= L) { cout << cnt << endl; return; }
        if(ansl == ansr) { cout << -1 << endl; return; }
        ansl = ansr;
    }
}

int main() {
    while(scanf("%d%d%d", &n, &L, &h) != EOF && n) {
        c = (h/2.0);
        init();
        solve();
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值