题意:有一块长为l,宽为w的草地,在其中心线有n个喷水装置,每个装置可喷出以p为中心以r为半径的圆,

选择尽量少的装置,把草地全部润湿。

析:我个去啊,做的真恶心,看起来很简单,实际上有n多个坑啊,首先这个题,应该可以看出来是贪心算法,

具体的说是区间覆盖问题,这个问题总体来说不难,但是在这有了巨多的坑。要注意以下几点:

1.这是一个草坪,不是线段,首先你要先把实验室转化为线段。

2.这个喷水装置喷出是圆,不是矩形,要运用数学知识进行运算。

3.输入的半径的两倍如果小于等于宽度,就得忽略不记。因为你算上也没有作用。

4.这个要用浮点数存,我没考虑精度,好歹也过了,还不算太坑。

这个图片是我从网上截的地址是http://blog.csdn.net/shuangde800/article/details/7828675

这个图片可以帮助理解。剩下的就和普通的区间覆盖没太大区别了。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm> using namespace std;
const int maxn = 10000 + 10; struct node{
double l, r;
node() { }
node(double ll, double rr) : l(ll), r(rr) { }
bool operator < (const node &p) const{
return l < p.l;
}
}; node a[maxn]; int main(){
// freopen("in.txt", "r", stdin);
int n;
double l, w;
while(~scanf("%d %lf %lf", &n, &l, &w)){
int indx = 0;
double p, r;
for(int i = 0; i < n; ++i){
scanf("%lf %lf", &p, &r);
double x = sqrt(r*r - (w/2.0)*(w/2.0));
if(2 * r > w) a[indx++] = node(p-x, p+x);
}
sort(a, a+indx);
double s = 0, e = 0;
int cnt = 1;
if(a[0].l > s){ printf("-1\n"); continue; } for(int i = 0; i < indx; ++i){
if(a[i].l <= s) e = max(e, a[i].r); //寻找最大区间
else{
++cnt; //记数
s = e; //更新s
if(a[i].l <= s) e = max(e, a[i].r);
else break; //无解,退出循环
}
if(e >= l) break; //提前结束
} if(e < l) printf("-1\n");
else printf("%d\n", cnt);
}
return 0;
}

UVa 10382 Watering Grass (区间覆盖贪心问题+数学)的更多相关文章

  1. UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】

    UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...

  2. UVA 10382 Watering Grass(区间覆盖,贪心)题解

    题意:有一块草坪,这块草坪长l 米,宽 w 米,草坪有一些喷头,每个喷头在横坐标为 p 处,每个喷头的纵坐标都是(w/2) ,并且喷头的洒水范围是一个以喷头为圆心,半径为 r 米的圆.每次最少需要打开 ...

  3. UVA 10382 Watering Grass (区间覆盖,贪心)

    问题可以转化为草坪的边界被完全覆盖.这样一个圆形就换成一条线段. 贪心,从中选尽量少的线段把区间覆盖,按照把线段按左端点排序,记录一个当前已经覆盖区间的位置cur, 从左端点小于等于cur选一个右端点 ...

  4. UVA 10382 Watering Grass 贪心+区间覆盖问题

    n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...

  5. UVA 10382 Watering Grass(区间覆盖)

    n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...

  6. uva 10382 - Watering Grass(区域覆盖问题)

    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 ...

  7. UVa 10382 - Watering Grass

    题目大意:有一条长为l,宽为w的草坪,在草坪上有n个洒水器,给出洒水器的位置和洒水半径,求能浇灌全部草坪范围的洒水器的最小个数. 经典贪心问题:区间覆盖.用计算几何对洒水器的覆盖范围简单处理一下即可得 ...

  8. UVa 10382 - Watering Grass 贪心,水题,爆int 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  9. UVA - 10382 Watering Grass(几何)

    题意:有一个矩形,n个圆.已知矩形的长宽和圆的半径,问最少需多少个圆将矩形完全覆盖. 分析: 1.首先求圆与矩形的长的交点,若无交点,则一定不能对用最少的圆覆盖矩形有贡献. 2.如果两个圆与矩形相交所 ...

随机推荐

  1. UGUI Auto Layout 自动布局

    Layout Element 首先分配 Minimum Size 如果还有足够空间,分配 Preferred Size 如果还有额外空间,分配 Flexible Size 比较特别的是 Flexibl ...

  2. [转载]百分之百自动登录2345王牌技术员联盟源代码(delphi)

    资源地址:http://download.csdn.net/detail/softlib/9670613

  3. struts2的运行流程

    流程: 1:url 提交到tomcat http://localhost/s2/firstAction 2:tomcat 根据工程名 去 webapps 文件夹下找到对应工程 3:找web.xml S ...

  4. js 获取input选择的图片的信息

    1JS $("#btn").click(function () { var imageEle = document.getElementById("images" ...

  5. Python 列表表达式 ,迭代器(2) Yield

    .yield 暂存为list def max_generator(numbers): current_max = for i in numbers: current_max = max(i, curr ...

  6. hdoj Max Sum Plus Plus(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意:----最大M子段和问题给定由 n个整数(可能为负整数)组成的序列a1,a2,a3,……, ...

  7. [leetcode]29. Divide Two Integers 两整数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  8. MongoDB使用场景和局限 (转)

    MongoDB的使用场景: 1.Web应用程序.文档能表示丰富的数据结构,建模相同数据库所需的集合数量通常会比使用完全正规化关系型数据库的数据表数量要少.动态查询和二级索引能让你轻松的实现SQL开发者 ...

  9. php单点登陆简单实现 (iframe方式)

    有四个网站分别为: www.a.com www.b.com www.c.com www.sso.com 需求是如果我们在sso登陆后,其他网站也会显示登陆中,不需要重复登陆,退出时,其他网站也会失效. ...

  10. cookies,sessionStorage 和 localStorage 的区别

    请描述一下 cookies,sessionStorage 和 localStorage 的区别? sessionStorage 和 localStorage 是HTML5 Web Storage AP ...