南阳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 ...
随机推荐
- 关于标准ui设计图转换为H5页面的终端适配
一些基本概念 在进行具体实战之前,首先得了解下面这些基本概念(术语): 视窗 viewport 简单的理解,viewport是严格等于浏览器的窗口.在桌面浏览器中,viewport就是浏览器窗口的宽度 ...
- 利用Java生成UUID
UUID是什么? UUID 是 通用唯一识别码(Universally Unique Identifier)的缩写,是一种软件建构的标准,亦为开放软件基金会组织在分布式计算环境领域的一部分.其目的,是 ...
- 解决 python 中,时间日期不能序列化的问题
在python 中, 你在数据库娶到了数据中如果含有时间日期,那么你在向前端作为json对象传递的时候呢,就会报错.大致如下: TypeError: datetime.datetime(2017, 1 ...
- c++ --> 你可能不知道的c++
你可能不知道的c++ 你可能不知道的 C++(一) 你可能不知道的 C++(二)
- 面试:Handler 的工作原理是怎样的?
面试场景 平时开发用到其他线程吗?都是如何处理的? 基本都用 RxJava 的线程调度切换,嗯对,就是那个 observeOn 和 subscribeOn 可以直接处理,比如网络操作,RxJava 提 ...
- java数组排序,并将数组内的数据求和
java数据编列并求和,江湖我狼哥,人狠话不多,直接上代码! import java.util.Arrays; public class Intarry { public static void ma ...
- hibernate框架学习笔记7:HQL查询、Criteria查询简介
HQL查询:hibernate独有的查询语言 适用于不复杂的多表查询 示例: 实体类: package domain; public class Customer { private Long cus ...
- Beta冲刺第六天
一.昨天的困难 没有困难. 二.今天进度 1.林洋洋:更新申请ip为域名,去除druid数据源统计 2.黄腾达:协作详情中添加成员对话框优化 3.张合胜:修复侧栏菜单mini状态下不能显示问题 三.明 ...
- Alpha冲刺No.4
冲刺Day4 一.站立式会议 本来还想今天下午好好弄弄安卓开发,结果计划赶不上变化.(不存在的) 完成备忘录设计,个人界面设计 二.实际项目进展 搞了404(安卓和ssm的连接),好像还是不太行. 备 ...
- python实现k-近邻算法
参考:<机器学习实战>- Machine Learning in Action 一. 必备的包 实现此算法需要准备以下的包: • matplotlib,用于绘图 • numpy,数组处理库 ...