UVA_10382

    喷水装置能否覆盖草坪,其实只取决于图中每个圆蓝色部分能否将草坪覆盖,这样我们就将问题转化成了有若干线段,求覆盖一个指定区间最少要多少条线段。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define MAXN 10010
const double eps = 1e-10;
int N, M;
double L, W;
struct Seg
{
    double x, y;
    bool operator < (const Seg &t) const
    {
        return x < t.x;
    }
}seg[MAXN];
int dcmp(double x)
{
    return (x > eps) - (x < -eps);
}
double sqr(double x)
{
    return x * x;
}
void input()
{
    M = 0;
    for(int i = 0; i < N; i ++)
    {
        double p, r;
        scanf("%lf%lf", &p, &r);
        if(dcmp(2 * r - W) <= 0) continue;
        double l = sqrt(sqr(r) - sqr(W * 0.5));
        seg[M].x = p - l, seg[M].y = p + l, ++ M;
    }
    std::sort(seg, seg + M);
}
int process()
{
    int cnt = 0;
    double x = 0, y = 0;
    for(int i = 0; i < M; i ++)
    {
        if(dcmp(seg[i].x - x) > 0)
        {
            if(dcmp(seg[i].x - y) > 0) return -1;
            ++ cnt, x = y;
            if(dcmp(x - L) >= 0) return cnt;
        }
        y = std::max(y, seg[i].y);
    }
    if(dcmp(y - L) < 0) return -1;
    return cnt + 1;
}
int main()
{
    while(scanf("%d%lf%lf", &N, &L, &W) == 3)
    {
        input();
        printf("%d\n", process());
    }
    return 0;
}

 

 

posted on 2012-11-08 14:23  Staginner  阅读(286)  评论(0编辑  收藏  举报