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个洒水器,给出洒水器的位置和洒水半径,求能浇灌全部草坪范围的洒水器的最小个数. 经典贪心问题:区间覆盖.用计算几何对洒水器的覆盖范围简单处理一下即可得 ...
随机推荐
- bzoj 1415 期望+记忆化搜索 ****
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdkAAAIfCAIAAACzfDFhAAAgAElEQVR4nOy9bVwTW57vm5fnhed+Pn
- bat学习
http://www.cnblogs.com/gaohongchen01/p/4042047.html http://www.cnblogs.com/amylis_chen/p/3585339.htm ...
- 在Salesforce中创建Web Service供外部系统调用
在Salesforce中可以创建Web Service供外部系统调用,并且可以以SOAP或者REST方式向外提供调用接口,接下来的内容将详细讲述一下用SOAP的方式创建Web Service并且用As ...
- 删除表数据drop、truncate和delete的用法
说到删除表数据的关键字,大家记得最多的可能就是delete了 然而我们做数据库开发,读取数据库数据.对另外的两兄弟用得就比较少了 现在来介绍另外两个兄弟,都是删除表数据的,其实也是很容易理解的 老大- ...
- JAVA Day7
6 方法 1.格式[访问控制符] void返回值类型 方法名(参数列表:数据类型 参数名); 2.类的方法: *用来定义类的某种行为或功能 * 3.方法的返回值 *如果有返回值,方法中必须要使用 ...
- I Hate It(线段树基础)
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- delphi override、overload、reintroduce的区别-0613.txt
http://blog.csdn.net/honglixx/article/details/3624934 1.override overload reintroduce的中文叫法是什么? overr ...
- AngularJS html+DOM+ng-click事件
ng-disabled 指令直接绑定应用程序数据到 HTML 的 disabled 属性. ng-show 指令用于设置应用部分是否可见. ng-show="true" 设置 HT ...
- 我的c++学习(1)hello world!
// texthello.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using na ...
- http://blog.csdn.net/z69183787/article/details/37819831
http://blog.csdn.net/z69183787/article/details/37819831