Watering Grass
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/E
题意:
给定一条草坪,草坪上有n个喷水装置。草坪长l米宽w米,n个装置都有每个装置的位置和喷水半径。要求出最少需要几个喷水装置才能喷满草坪。喷水装置都是装在草坪中间一条水平线上的。
案例:
Sample Input
8 20 2
5 3
4 1
1 2
7 2
10 2
13 3
16 2
19 4
3 10 1
3 5
9 3
6 1
3 10 1
5 3
1 1
9 1
Sample Output
6
2
-1
分析:
区间覆盖问题
用贪心,按覆盖最远的排序,每次找最远的然后更行左边界。
这题有个要注意的地方是圆型覆盖,所以在初始化时把其化成矩形覆盖问题,(根据勾股定理转化)
#include<iostream>
#include<cstdio>
#include<cstring>
#include <algorithm>
#include<cmath>
using namespace std;
#define maxn 10100
int n,k;
double m,w,x,y;
struct node
{
double l;
double r;
}a[maxn];
int cmp( node a, node b) //按覆盖最远的排序(半径由大到小)
{ return a.r > b.r; }
int main()
{
while(scanf("%d%lf%lf",&n,&m,&w)!=EOF)
{
double L=;
k=;
int i=,j=;
double d;
while(n--)
{
scanf("%lf%lf",&x,&y);
d=sqrt(y*y-(w*w)/); //转化为矩形覆盖
a[j].l=x-d;
a[j++].r=x+d; }
sort(a,a+j,cmp);
while(L<m)
{ int i;
for(i=;i<j;i++)
{
if(a[i].l<=L&&a[i].r>L)
{
L=a[i].r;
k++;
break;
}
}
if(i==j) break;
}
if(L<m)
cout<<"-1"<<endl;
else
cout<<k<<endl;
}
return ;
}
Watering Grass的更多相关文章
- 10382 - Watering Grass
Problem E Watering Grass Input: standard input Output: standard output Time Limit: 3 seconds n sprin ...
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
- Watering Grass(贪心)
Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long and w meters ...
- UVA 10382 Watering Grass(区间覆盖)
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...
- UVA 10382 Watering Grass 贪心+区间覆盖问题
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...
- Watering Grass(贪心算法)
给定一条草坪.草坪上有n个喷水装置.草坪长l米宽w米..n个装置都有每个装置的位置和喷水半径..要求出最少需要几个喷水装置才能喷满草坪..喷水装置都是装在草坪中间一条水平线上的. n sprinkle ...
- Uva 10382 (区间覆盖) Watering Grass
和 Uva 10020几乎是一样的,不过这里要把圆形区域转化为能够覆盖的长条形区域(一个小小的勾股定理) 学习一下别人的代码,练习使用STL的vector容器 这里有个小技巧,用一个微小量EPS来弥补 ...
- uva 10382 - Watering Grass(区域覆盖问题)
Sample Input 8 20 2 5 3 4 1 1 2 7 2 10 2 13 3 16 2 19 4 3 10 1 3 5 9 3 6 1 3 10 1 5 3 1 1 9 1 Sample ...
- UVa 10382 - Watering Grass
题目大意:有一条长为l,宽为w的草坪,在草坪上有n个洒水器,给出洒水器的位置和洒水半径,求能浇灌全部草坪范围的洒水器的最小个数. 经典贪心问题:区间覆盖.用计算几何对洒水器的覆盖范围简单处理一下即可得 ...
随机推荐
- jquery获取、改变元素属性值
//标签的属性称作元素属性,在JS里对应的DOM对象的对应属性叫DOM属性.JS里的DOM属性名有时和原元素属性名不同. //==================================操作元 ...
- Linux命令之ar - 创建静态库.a文件和动态库.so
转自:http://blog.csdn.net/eastonwoo/article/details/8241693 用途说明 创建静态库.a文件.用C/C++开发程序时经常用到,但我很少单独在命令行中 ...
- 3D建模与处理软件简介
[前言]自半年前笔者发表博客“什么是计算机图形学”以来,时常有人来向笔者询问3D模型的构建方法与工具.笔者的研究方向是以3D技术为主,具体包括3D建模,3D处理及3D打印三个方面,在3D建模与处理方面 ...
- 解释一下SQLSERVER事务日志记录
解释一下SQLSERVER事务日志记录 大家知道在完整恢复模式下,SQLSERVER会记录每个事务所做的操作,这些记录会存储在事务日志里,有些软件会利用事务日志来读取 操作记录恢复数据,例如:log ...
- VS2013安装oepncv2.4.10 以及opencv 3.0.0
Author:Maddock Date:2014.12.27 …………………………………………………………………………………………………… PS: VS2013 + OPENCV 3.0.0 的安装, ...
- git warning: LF will be replaced by CRLF in...
如果你有git项目,在提交代码的过程中可能会碰到上面的警告,特别是的项目中包含序列化对象的时候,你可能要小心!! 警告的含义是说换行符的,不同的操作系统的换行符是不一致的,如果你不清楚,真得看看这个 ...
- KMP模式匹配算法
KMP模式匹配算法 相信很多人对于这个还有点不了解,或者说是不懂,下面,通过一道题,来解决软考中的这个问题! 正题: aaabaaa,其next函数值为多少? 对于这个问题,我们应该怎么做呢? 1.整 ...
- 记录Android Studio项目提交到github上的出错处理
首先是按照网上的教程进行了一次提交,具体见http://web.gxzj.com.cn/News.aspx?id=325505 记得当时出现过这个错误Can't connect to reposito ...
- LoadRunner11录制APP脚本(1)
1.测试准备: a.首先安装LoadRunner11.0的版本跟新
- C# 溢出检查
checked: byte b = 255; checked { b++; } Console.WriteLine(b.ToString()); 执行出错:算术运算导致溢出. unchecked: b ...