高效算法——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, ...
随机推荐
- 利用MutationObserver对页面元素的改变进行监听
'use strict'; let MutationObserver = window.MutationObserver || window.WebKitMutationObserver || win ...
- ASP.NET Excel数据导出数据库
/// <summary> /// 根據gridview導出excel /// </summary> /// <param name="ctl"> ...
- java 从jar包中读取资源文件
在代码中读取一些资源文件(比如图片,音乐,文本等等),在集成环境(Eclipse)中运行的时候没有问题.但当打包成一个可执行的jar包(将资源文件一并打包)以后,这些资源文件找不到,如下代码: Jav ...
- OpenCV中Mat的详解
每次碰到Mat都得反复查具体的用法,网上的基础讲解不多,难得看到一篇,赶快转来收藏~ 原文地址:http://www.opencvchina.com/thread-1039-1-1.html 目标 我 ...
- javascript——归并方法
<script type="text/javascript"> //ECMAScript5 还新增了2个归并数组的方法:reduce()和reduceRight(). ...
- nuc970连接jlink进行单步调试的设置
在 USB mode 下, 先跟 NuWriter 接上, 然后用以下的设定. 按 Keil 的 debug (不是 download to flash)就可以接上了.
- free 堡垒机
环境: centos6.5 mini安装 iptables selinux已经关闭 jumpserver: 192.168.1.209 testserver: 192.168.1.210 一. 部署l ...
- 16_用LVM扩展xfs文件系统(当分区空间不够时)
1. 查看当前卷组空间(volume group)使用情况 [root@localhost ~]# vgdisplay 从下面的代码中发现剩余空间为0 --- Volume group --- VG ...
- 百度分享 ajax 或分页后显示不出问题解决方案
自从用了AJAX后,JS重新加载问题就如家常便饭般层出不穷啊.没有系统学习过js感觉亚历山大. 百度后,还是找到了解决办法. 百度分享创建了一个全局对象window._bd_share_main.通过 ...
- YII 小部件 解决多选按钮和单选按钮不在同一水平上 'separator'=>' '
主要是添加separator属性(这里)$hoddy,$sex在控制器里面定义的数组,然后render传过来的 <td> <?php echo $form-> ...