E - 贪心-- 区间覆盖

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E

解题思路:

贪心思想,将问题转化为区间覆盖问题,将草地的上边界作为要覆盖的区间,计算出每个洒水器覆盖的区间范围,不能覆盖的舍去,然后将洒水器按覆盖范围的左边界升序排列。

要覆盖的最右边的点right的初始值为0,遍历洒水器,找一个能覆盖住right且覆盖范围的右边界最大的洒水器,然后将该洒水器覆盖的右边界作为新的right,重复刚才的过程,直到覆盖整个草地。

程序代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define MAX 10005 struct node
{
double left, right;
bool operator <(const node &a) const
{
return left < a.left;
}
}p[MAX]; int n, num;
double l, w; int main()
{
int i;
// freopen("input.txt", "r", stdin);
while(scanf("%d %lf %lf", &n, &l, &w) != EOF)
{
num = ;
double po, r;
for(i = ; i < n; ++i)
{
scanf("%lf %lf", &po, &r);
if(r <= w/) continue;
double t = sqrt(r*r-w*w/4.0);
p[num].left = po-t;
p[num++].right = po+t;
} sort(p, p+num);
double left = , right = ;
bool flag = false;
int result = ;
i = ;
if(p[].left <= left)
{
while(i < num)
{
int j = i;
while(j < num && left >= p[j].left)
{
if(p[j].right > right)
right = p[j].right;
++j;
}
if(j == i) break;
result++;
left = right;
i = j;
if(left >= l)
{
flag = true;
break;
}
}
} printf("%d\n", flag ? result : -);
}
return ;
}

高效算法——E - 贪心-- 区间覆盖的更多相关文章

  1. 【题解】Cut the Sequence(贪心区间覆盖)

    [题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...

  2. 51nod 1091 线段的重叠【贪心/区间覆盖类】

    1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...

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

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

  4. 南阳OJ-12-喷水装置(二)贪心+区间覆盖

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=12 题目大意: 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有 ...

  5. nyoj 12——喷水装置二——————【贪心-区间覆盖】

    喷水装置(二) 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的 ...

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

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

  7. B. Heaters 思维题 贪心 区间覆盖

    B. Heaters 这个题目虽然只有1500的分数,但是我还是感觉挺思维的,我今天没有写出来,然后看了一下题解 很少做这种区间覆盖的题目,也不是很擅长,接下来讲讲我看完题解后的思路. 题目大意是:给 ...

  8. 高效算法——D 贪心,区间覆盖问题

    Given several segments of line (int the X axis) with coordinates [Li , Ri ]. You are to choose the m ...

  9. UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)

     Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinates [Li, ...

随机推荐

  1. Shell - 文件运算符

    文件运算符  文件运算符  描述 -b file  检测 file 是否为块设备文件 -c file  检测 file 是否为字符设备文件  -d file  检测 file 是否为目录 -e fil ...

  2. Visual Studio中Js使用智能感知

    使用了第三方的JS库或框架,在VS中编写JS代码,发现真是个悲剧,完全只能手打,智能感知没了,这不符合VS的一贯做风只要在写代码的JS文件加上以下代码,就可以有智能感知了 ///<referen ...

  3. MSSQLSERVER未分离LDF删除情况下的MDF附加

    经过网上资料搜索,此方法可以解决. LDF日志不要轻易删除,恢复主数据要用到,如果删除,记得先分离,然后移动到另外的地方. 下面是针对未分离删除日志文件,MDF文件附加,提示找不到日志的问题的解决方法 ...

  4. [转载]CentOS6.4+Mono3.0.7+Jexus5.2.5

    本文章来自互联网,但是本人已经在VM虚拟机里面测试成功,所以分享给大家 1.更新 yum -y update 2.安装Mono源码安装需要的库 yum -y install gcc gcc-c++ a ...

  5. maclean-【性能调优】Oracle AWR报告指标全解析 学习笔记

    原文链接:http://www.askmaclean.com/archives/performance-tuning-oracle-awr.html AWR小技巧 手动执行一个快照: Exec dbm ...

  6. Xcode断点的一些黑魔法

    转自 只会左键断点?是时候试试这样那样断点了 编码不能没调试,调试不能没断点(Break Point).XCode的断点功能也是越来越强大. 基本断点 如下图,这种是最常用的断点,也是最容易设置.左键 ...

  7. Syntax error on token "package", assert expected------踩坑记录

    今天写程序碰到个坑,eclipse编辑,jdk1.7,clean编译项目后报错Syntax error on token "package", assert expected 反复 ...

  8. ios开发之ios9UIWebView不显示网页问题

    错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...

  9. js浏览器键盘事件控制(转自新浪微博)

    js键盘事件全面控制 主要分四个部分第一部分:浏览器的按键事件第二部分:兼容浏览器第三部分:代码实现和优化第四部分:总结 第一部分:浏览器的按键事件 用js实现键盘记录,要关注浏览器的三种按键事件类型 ...

  10. C# WinFrom 编写正则表达式验证类

    public class ValidationRegex { /// <summary> /// 正则表达式字符串 /// </summary> public static s ...