uva 10382 Watering Grass

题意:求最少的圆能完全覆盖要求的矩形,其实一个圆有用的部分是与矩形相交的点所构成的矩形,因为多出来的部分还是要其他圆来填满,所以我们就建一个结构体保存圆所形成矩形的左,右坐标,然后就是贪心啦,求已覆盖范围内,最远的矩形添进来

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 10005;

double len,wid,x[MAXN],r[MAXN];
int n,cnt;
struct node
{
    double l;
    double r;
}arr[MAXN];

bool cmp(node a,node b)
{
    return a.l < b.l;
}

int main()
{
    while (scanf("%d",&n) != EOF)    
    {
        scanf("%lf%lf",&len,&wid);
        cnt = 0;
        for (int i = 1; i <= n; i++)
        {
            cnt++;
            scanf("%lf%lf",&x[cnt],&r[cnt]);
            if (r[cnt]*2 <= wid)
                cnt--;
        }
        n = cnt;
        for (int i = 1; i <= n; i++)
        {
            double d = sqrt(r[i]*r[i]-wid*wid*0.25);
            arr[i].l = x[i] - d;
            arr[i].r = x[i] + d;
        }

        sort(arr+1,arr+1+n,cmp);
        if (arr[1].l > 0.0)
            printf("-1\n");
        else
        {
            int ans = 0, cur = 1;
            double end = 0.0,maxr = -1.0;
            while (cur <= n)
            {
                while (arr[cur].l <= end && cur <= n)  //找到小于当前最远的最靠右边的矩形
                {
                    maxr =max(maxr,arr[cur].r);
                    cur++;
                }
                end = maxr;
                ans++;
                if (end > len)
                    break;
                if (arr[cur].l > end)  //如果有一个猛的跳到end外,就结束了
                    break;
            }
            if (end < len)
                printf("-1\n");
            else printf("%d\n",ans);
        }
    }
    return 0;
}



你可能感兴趣的:(uva 10382 Watering Grass)