南阳OJ-12-喷水装置(二)贪心+区间覆盖
题目链接:
http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=12
题目大意:
有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的喷水装置,每个喷水装置i喷水的效果是让以它为中心半径为Ri的圆都被润湿。请在给出的喷水装置中选择尽量少的喷水装置,把整个草坪全部润湿。
传送门:喷水装置(一)
思路:
区间覆盖问题,每个点有一段喷水区间,按照左端点排序即可,设置两变量begin, end,依次扫过去,每次取覆盖begin的区间更新end,如果没能覆盖begin,说明已经到了当前的最右端,begin=end,再次扫描,如果每次扫描开始的时候就不能覆盖begin,则无解,如果每次扫描的时候没有找到合适的end,也无解。如果最终的begin<l也表示到达不了终点。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn = 1e4 + ;
int T, n;double l, w;
struct node
{
double x, y;
bool operator < (const node a)const
{
return x < a.x;
}
node(){}
node(double x, double y):x(x), y(y){}
};
node a[maxn];
int main()
{
cin >> T;
while(T--)
{
cin >> n >> l >> w;
w = w * 0.5;
int tot = ;
double x, r;
for(int i = ; i < n; i++)
{
cin >> x >> r;
if(r <= w)continue;
double c = sqrt(1.0 * r * r - w * w);
a[tot].x = 1.0 * x - c;
a[tot++].y = 1.0 * x + c;
if(a[tot - ].x > l || a[tot - ].y < )tot--;
}
sort(a, a + tot);
double begin = , end = -;
int ans = ;
for(int i = ; i < tot && begin < l;)
{
if(a[i].x > begin)
{
ans = ;
break;
}
while(i < tot && a[i].x <= begin)//覆盖begin的区间更新出最大的end
{
end = max(end, a[i].y);
i++;
}
if(end < )//说明没有找到合适的区间,直接无解
{
ans = ;
break;
}
ans++;
begin = end;//更新两者
end = -;
}
if(begin < l)ans = ;
cout<<ans<<endl;
}
return ;
}
南阳OJ-12-喷水装置(二)贪心+区间覆盖的更多相关文章
- 高效算法——E - 贪心-- 区间覆盖
E - 贪心-- 区间覆盖 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E 解题思路: 贪心思想, ...
- 【题解】Cut the Sequence(贪心区间覆盖)
[题解]Cut the Sequence(贪心区间覆盖) POJ - 3017 题意: 给定一大堆线段,问用这些线段覆盖一个连续区间1-x的最小使用线段的数量. 题解 考虑一个这样的贪心: 先按照左端 ...
- nyoj 12——喷水装置二——————【贪心-区间覆盖】
喷水装置(二) 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<=10000)个点状的 ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
- 51nod 1091 线段的重叠【贪心/区间覆盖类】
1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...
- 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的分数,但是我还是感觉挺思维的,我今天没有写出来,然后看了一下题解 很少做这种区间覆盖的题目,也不是很擅长,接下来讲讲我看完题解后的思路. 题目大意是:给 ...
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
- hdoj 4883 TIANKENG’s restaurant【贪心区间覆盖】
TIANKENG’s restaurant Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/O ...
随机推荐
- 二叉树遍历等基本操作(Java实现)
前中后序遍历递归实现+层序遍历: 树的结点类代码: public class TreeNode<Value extends Comparable<? super Value>> ...
- Object源码解析(JDK1.8)
package java.lang; public class Object { /** * 一个本地方法,具体是用C(C++)在DLL中实现的,然后通过JNI调用 */ private static ...
- ASP.NET MVC编程——验证、授权与安全
1 验证 一般采用表单验证完成登陆验证,建议结合SSL使用.为限制控制器只能执行HTTPS,使用RequireHttpsAttribute 2 授权 对账户的权限的控制可以通过在控制器或控制器操作上 ...
- windows 设置/修改全局快捷键
打开控制面板,小图表显示下 点击 管理工具项, 将自己想要谁知快捷键的程序的快捷方式放进去,(需要确认管理员权限) 如图,第一个即为 lz添加的 右击选择属性 在快捷键处同时按下你想要的组合键即可(不 ...
- 笔记:MyBatis Mapper XML文件详解 - Cache
缓存(Cache) 从数据库中加载的数据缓存到内存中,是很多应用程序为了提高性能而采取的一贯做法.MyBatis对通过映射的SELECT语句加载的查询结果提供了内建的缓存支持.默认情况下,启用一级缓存 ...
- 3.Properties文件的加载和使用
一.Properties简介 Properties 类继承自HashTable,提供的方法很像Map的实现类HashMap.它在 Java 编程的早期就有了,并且几乎没有什么变化.J2SE 的 Tig ...
- [poj3468]A Simple Problem with Integers_线段树
A Simple Problem with Integers 题目大意:给出n个数,区间加.查询区间和. 注释:1<=n,q<=100,000.(q为操作次数). 想法:嗯...学了这么长 ...
- kvm之三:本地安装虚拟机
1.格式化新添加的磁盘 [root@kvm ~ ::]#fdisk /dev/sdb Command (m for help): n //新建分区 Command action e extended ...
- lua continue实现
--第一种 , do while true do == then break end -- 这里有一大堆代码 -- -- break end end --第二种 i = ) do if () then ...
- Java学习日记——基本数据类型
基本数据类型: byte 1个字节 正负都能表示2的8-1次方 -128~127(包括0) short 2个字节 2的16-1次 整数类型 (默认为int类型) int 4个字节 2的32-1次方 l ...