UVa 10382:Watering Grass

链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1323

原题:

n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance from the left end of the center line and its radius of operation.

What is the minimum number of sprinklers to turn on in order to water the entire strip of grass?

Input

Input consists of a number of cases. The first line for each case contains integer numbers n, l and w with n <= 10000. The next n lines contain two integers giving the position of a sprinkler and its radius of operation. (The picture above illustrates the first case from the sample input.)

Output

For each test case output the minimum number of sprinklers needed to water the entire strip of grass. If it is impossible to water the entire strip output -1.

Sample input

8 20 2

5 3

4 1

1 2

7 2

10 2

13 3

16 2

19 4

3 10 1

3 5

9 3

6 1

3 10 1

5 3

1 1

9 1

Sample output

6

2

-1

题目大意:

有一块草坪,长为l,宽为w,在它的水平中心线上有n个位置可以安装喷水装置,各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆。求出最少需要的喷水装置个数。

分析与总结:

这题的关键在于转化

本栏目更多精彩内容:http://www.bianceng.cn/Programming/sjjg/

根据这图可以看出,一个喷水装置的有效覆盖范围就是圆中间的那个矩形。所以,在输入的同时,进行预处理,转换成矩形左边的坐标和右边的坐标。 这样,其实就转换成了经典的区间覆盖问题。

代码:

/*
 * UVa: 10382 - Watering Grass
 * 贪心:最小覆盖
 * Result: Accept
 * Time: 0.016s
 * Author: D_Double
 */
#include<iostream>
#include<cmath>
#include<cstdio>
#include<algorithm>
#define MAXN 10005
using namespace std;
int n,nIndex;
double l, w;  

struct Node{
    double left;
    double right;
    friend bool operator < (const Node&a,const Node&b){
        return a.left < b.left;
    }
}arr[MAXN];  

int main(){
    double p,r;
    while(scanf("%d%lf%lf",&n,&l,&w)!=EOF){
        nIndex=0;
        for(int i=0; i<n; ++i){
            scanf("%lf%lf",&p,&r);
            if(w/2>=r)
                continue; //直径小于宽度的不考虑
            double t=sqrt(r*r-w*w/4.0);
            arr[nIndex].left=p-t;
            arr[nIndex].right=p+t;
            ++nIndex;
        }  

        sort(arr,arr+nIndex);
        int cnt=0;
        double left=0, right=0;
        bool flag=false;  

        if(arr[0].left <= 0 ){
            int i=0;  

            while(i < nIndex){  

                int j=i;
                while(j<nIndex && left>=arr[j].left){
                    if(arr[j].right > right)
                        right=arr[j].right;
                    ++j;
                }
                if(j==i) break;  // 如果上面的循环体没有执行,说明之后都没有满足的了  

                ++cnt;
                left=right;
                i=j;  

                if(left>=l){
                    flag=true;
                    break;
                }
            }
        }
        if(flag) printf("%d\n", cnt);
        else printf("-1\n");
    }
    return 0;
}

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索uva
, center
, line
, strip
, of
, The
Installed
ora 10382、西安人造草坪hygrass、人造草坪hygrass、grass、grassroots,以便于您获取更多的相关知识。

时间: 2024-12-23 18:27:56

UVa 10382:Watering Grass的相关文章

uva 10382 - Watering Grass

点击打开链接uva 10382 题目意思:     有一块草坪,长为l,宽为w  ,在它的中心线上装有n个点状的喷水装置 , 效果是让以它为中心半径为ri的圆被润湿  , 选择尽量少的喷水装置把整个草坪全部润湿. 解题思路:   1:贪心,区间覆盖 2:这道题就是一个区间覆盖的变形,只是没有那么的直观而已.刚开始区间求错了,直接求出最左边和最右边的左边以此来作为区间端点,后来WA了近20次才发现考虑错了,嗨,只怪自己思维不够严谨.真正的区间求法是,把这个圆和这个矩形两边的交点的横坐标求出来就是区

UVa 1422:Processor 任务处理问题

题目大意:有n个任务送到处理器处理,每个任务信息包括r,d,w,r代表开始时间,w代表必须要结束的时间,w指需要多少时间处理. 其中处理器的处理速度可以变速,问处理器最小需要多大速度才能完成工作? 输入: 3 5 1 4 2 3 6 3 4 5 2 4 7 2 5 8 1 6 1 7 25 4 8 10 7 10 5 8 11 5 10 13 10 11 13 5 8 15 18 10 20 24 16 8 15 33 11 14 14 1 6 16 16 19 12 3 5 12 22 25

UVa 10905:Children&#039;s Game

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1846 类型: 排序 There are lots of number games for children. These games are pretty easy to play but not so easy to make. We will

UVa 10763:Foreign Exchange

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1704 原题: Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very succes

UVa 10341: Solve It

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1282 原题: Solve the equation:        p*e-x + q*sin(x) + r*cos(x) + s*tan(x) + t*x2 + u = 0        where 0 <= x <= 1. Input

UVa 10057:A mid-summer night&#039;s dream.

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=998 原题: This is year 2200AD. Science has progressed a lot in two hundred years. Two hundred years is mentioned here because thi

UVa 10487:Closest Sums

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1428 原题: Given is a set of integers and then a sequence of queries. A query gives you a number and asks to find a sum of two di

UVa 10340:All in All

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1281 原题: You have devised a new encryption technique which encodes a message by inserting between its characters randomly gener

UVa 10700:Camel trading

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1641 原题: Background Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had its origin in the