高效算法——E - 贪心-- 区间覆盖
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 - 贪心-- 区间覆盖的更多相关文章
- 【题解】Cut the Sequence(贪心区间覆盖)
[题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...
- 51nod 1091 线段的重叠【贪心/区间覆盖类】
1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
- 南阳OJ-12-喷水装置(二)贪心+区间覆盖
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=12 题目大意: 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有 ...
- nyoj 12——喷水装置二——————【贪心-区间覆盖】
喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的 ...
- UVA 10382 Watering Grass 贪心+区间覆盖问题
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...
- B. Heaters 思维题 贪心 区间覆盖
B. Heaters 这个题目虽然只有1500的分数,但是我还是感觉挺思维的,我今天没有写出来,然后看了一下题解 很少做这种区间覆盖的题目,也不是很擅长,接下来讲讲我看完题解后的思路. 题目大意是:给 ...
- 高效算法——D 贪心,区间覆盖问题
Given several segments of line (int the X axis) with coordinates [Li , Ri ]. You are to choose the m ...
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
随机推荐
- 分享最近写的 两条sql语句
1. 搭建基本环境 插入测试数据 insert into jgdm (jgdm,jgmc) values('12300000000','河南省');insert into jgdm (jgdm,jg ...
- ORACLE 数据库简单测试
ORACLE 数据库简单测试 操作系统:Windows 7 – ORACLE:oracle database 10.2.0.4 一.目的 测试 启动监听程序.数据库 非同一个用户的情况,用户是否可以 ...
- [转]Delphi中ShellExecute的妙用
Delphi中ShellExecute的妙用 ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件.打开一个目录.打印一个文件等等),并对外部程序有一定的控制. ...
- python中如何使用help命令?
python下 help()使用方法 查看python所有的modules:help("modules") 单看python所有的modules中包含指定字符串的modules ...
- php 备份和还原数据库
ignore_user_abort();//关掉浏览器,PHP脚本也可以继续执行. set_time_limit(0);// 通过set_time_limit(0)可以让程序无限制的执行下去 $int ...
- linux搜索命令
1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> ...
- Oracle数据库之rownum
1. 介绍 当我们在做查询时,经常会遇到如查询限定行数或分页查询的需求,MySQL中可以使用LIMIT子句完成,在MSSQL中可以使用TOP子句完成,那么在Oracle中,我们如何实现呢? Oracl ...
- InvalidArgument=Value of '1' is not valid for 'index'
用ListView实现点击ListView的项删除该项的效果,调用ItemSelectionChanged事件. 代码如下: private void listView1_ItemSelectionC ...
- js事件处理 —— 详解
对于JS事件处理分为四部分: 1.html事件处理程序 直接添加到HTML结构中 解析:用html处理程序可以直接在button元素里直接调用,但是维护性不是很方便 <!DOCTYPE html ...
- css杂项,清除浮动
在写HTML代码的时候,发现在Firefox等符合W3C标准的浏览器中,如果有一个DIV作为外部容器,内部的DIV如果设置了float样式,则外部的容器DIV因为内部没有clear,导致不能被撑开.看 ...