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个洒水器,给出洒水器的位置和洒水半径,求能浇灌全部草坪范围的洒水器的最小个数. 经典贪心问题:区间覆盖.用计算几何对洒水器的覆盖范围简单处理一下即可得 ...
随机推荐
- ImageSwitcher自定意效果+定时切换图片
Activity实现 1 import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; ...
- list[C++]
//双向链表 #include <iostream> using namespace std; #include <list> int main(int argc, const ...
- ubuntu中jdk已经安装,但是eclipse启动报错
问题描述 在ubuntu中,jdk已经正常安装,java_home变量已经配置,但是启动eclipse的时候还是弹出以下错误信息: A Java RunTime Environment (JRE) o ...
- loj 1337
题目链接:http://lightoj.com/volume_showproblem.php?problem=1337 思路:对于搜过的区域进行标记,如果要求的点落在已经搜过的区域,那么直接取出来即可 ...
- Linux学习笔记(6)Linux常用命令之帮助命令与用户管理命令
(1)man man命令用于获得命令或配置文件的帮助信息,英文原意为manual,所在路径为/usr/bin/man,其语法格式为: man [命令或配置文件] 注意:查看配置文件的帮助信息时无需绝对 ...
- SSH 小总
SSH 为 struts+spring+hibernate的一个集成框架,是目前较流行的一种Web应用程序开源框架. 集成SSH框架的系统从职责上分为四层:表示层.业务逻辑层.数据持久层和域模块层,以 ...
- VisualStudio一打开工程就崩溃-重打开output显示We were unable to automatically populate your Visual Studio Online accounts.
and this method exactly effected on me
- java 线程演示
package unit8; public class Mainthread { public static void main(String[] args) { Thread t = new Thr ...
- u盘中放入大于4g单独文件失败解决
u盘中文件格式通常为FAT32,需要转换为NTFS格式 在win7中cmd调出命令行,输入: convert H: /fs:ntfs (H为u盘位置)
- JavaScript设计模式——前奏(封装和信息隐藏)
前面一篇讲了js设计模式的前奏,包括接口的讲解.. 三:封装和信息隐藏: 信息隐藏用来进行解耦,定义一些私有的数据和方法. 封装是用来实现信息隐藏的技术,通过闭包实现私有数据的定义和使用. 接口在这其 ...