UVA 10382.Watering Grass-贪心
10382 - Watering Grass
Time limit: 3.000 seconds
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance from the left end of the center line and its radius of operation.
What is the minimum number of sprinklers to turn on in order to water the entire strip of grass?
Input
Input consists of a number of cases. The first line for each case contains integer numbers n, l and w with n ≤ 10000. The next n lines contain two integers giving the position of a sprinkler and its radius of operation. (The picture above illustrates the first case from the sample input.)
Output
For each test case output the minimum number of sprinklers needed to water the entire strip of grass. If it is impossible to water the entire strip output ‘-1’.
Sample Input
8 20 2
5 3
4 1
1 2
7 2
0 2
13 3
16 2
19 4
3 10 1
3 5
9
6 1
3 10 1
5 3
1 1
9 1
Sample Output
6
2
-1
题意就是浇水,整片草地都要浇上水,装置在草地水平中间的线上,要注意的是不是圆的面积,是近似矩形的面积。
然后就是直接算长度就可以,要求装置洒水的半径要>草地一半的宽度。
然后长度的话也不是圆的半径,是图上红色的长度。(图画大了,挠头。。。)
关键就是左右两边的长度计算,精度很重要,打训练赛的时候就是卡死在精度这里,wa了8发,gg。
a[i].l=max(0.0,w-sqrt((double)r*r-(double)s*s/4.0));
a[i].r=min((double)l,w+sqrt((double)r*r-(double)s*s/4.0));
就是这个,还是一个大佬帮我改对的。
代码:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
struct node{
double l,r;
}a[N];
double cmp(node a,node b){
return a.r>b.r;
}
int main(){
int n,num,l,s,w,r;
double p;
while(~scanf("%d%d%d",&n,&l,&s)){
p=0.0;num=;
for(int i=;i<n;i++){
scanf("%d%d",&w,&r);
if(r>=s/2.0){
a[i].l=max(0.0,w-sqrt((double)r*r-(double)s*s/4.0)); //wa在这里了。
a[i].r=min((double)l,w+sqrt((double)r*r-(double)s*s/4.0));}
}
sort(a,a+n,cmp);
//for(int i=0;i<n;i++) cout<<a[i].l<<" "<<a[i].r<<endl;
while(p<l){
int i;
for(i=;i<n;i++){
if(a[i].l<=p&&a[i].r>p){
p=a[i].r;
num++;
break;
}
}
if(i==n)break;
}
if(p<l) printf("-1\n");
else printf("%d\n",num);
}
return ;
}
网上好多代码都是错的,卡精度上了。
==
然后就是NYOJ上有一道贪心长得差不多,但是比上面的水。。。
UVA 10382.Watering Grass-贪心的更多相关文章
- UVa 10382 - Watering Grass 贪心,水题,爆int 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 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【贪心+区间覆盖问题+高精度】
UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...
- UVA 10382 Watering Grass(区间覆盖,贪心)题解
题意:有一块草坪,这块草坪长l 米,宽 w 米,草坪有一些喷头,每个喷头在横坐标为 p 处,每个喷头的纵坐标都是(w/2) ,并且喷头的洒水范围是一个以喷头为圆心,半径为 r 米的圆.每次最少需要打开 ...
- UVa 10382 Watering Grass (区间覆盖贪心问题+数学)
题意:有一块长为l,宽为w的草地,在其中心线有n个喷水装置,每个装置可喷出以p为中心以r为半径的圆, 选择尽量少的装置,把草地全部润湿. 析:我个去啊,做的真恶心,看起来很简单,实际上有n多个坑啊,首 ...
- UVA 10382 Watering Grass (区间覆盖,贪心)
问题可以转化为草坪的边界被完全覆盖.这样一个圆形就换成一条线段. 贪心,从中选尽量少的线段把区间覆盖,按照把线段按左端点排序,记录一个当前已经覆盖区间的位置cur, 从左端点小于等于cur选一个右端点 ...
- UVa 10382 - Watering Grass
题目大意:有一条长为l,宽为w的草坪,在草坪上有n个洒水器,给出洒水器的位置和洒水半径,求能浇灌全部草坪范围的洒水器的最小个数. 经典贪心问题:区间覆盖.用计算几何对洒水器的覆盖范围简单处理一下即可得 ...
- 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(区域覆盖问题)
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_贪心
题意:给你个矩形n*m,再给你n个圆的圆心坐标和半径,问最用最少用几个圆把这个矩形覆盖 思路:直接想发现这问题不容易,后来发现可以把圆看做区间(能把矩形面积覆盖),然后这个问题就容易解决了 #incl ...
随机推荐
- delphi保存文件的命名规则
没有固定的标准.自己可以定义 .你可以参考PASCAL命名法则.查一下PASCAL命名. 我习惯用UMain,FMain,UDM,DM,UAboutBox,AboutBox.....程序相关内容都放在 ...
- 【bzoj1465/bzoj1045】糖果传递 数论
题目描述 老师准备了一堆糖果, 恰好n个小朋友可以分到数目一样多的糖果. 老师要n个小朋友去拿糖果, 然后围着圆桌坐好, 第1个小朋友的左边是第n个小朋友, 其他第i个小朋友左边是第i-1个小朋友. ...
- ScrollBarsEnabled的使用
在WinForm中通过WebBrowser获取网页,我想把WebBrowser的ScollBar去掉,我的网页不需要滚动条. 设置方法如下:单击WebBrowser设计页面,在属性页面有一个Scrol ...
- BZOJ4327 JSOI2012玄武密码(AC自动机)
当然可以在SA上二分答案,但看起来会被卡log.考虑对模板串建出AC自动机,用母串在上面跑,标记上所有能到达的点.注意到达某个点时需要标记所有其通过fail指针可以走到的点,如果遇到一个标记过的点就可 ...
- 【题解】Bzoj2560串珠子
挺强的……容斥+状压DP.首先想到如果可以求出f[k],f[k]代表联通状态为k的情况下的合法方案数,则f[k] = g[k] - 非法方案数.g[k]为总的方案数,这是容易求得的.那么非法方案数我们 ...
- vue中使用 echarts3.0 或 echarts2.0 (模拟迁徙图,折线图)
一.echarts3.0(官网: http://echarts.baidu.com/) 首先通过npm安装echarts依赖,安装的为3.0版本 npm install echarts -s 也可以使 ...
- HTTP请求中同步与异步有什么不同
普通的B/S模式就是同步,而AJAX技术就是异步,当然XMLHttpReques有同步的选项. 同步:提交请求->等待服务器处理->处理完毕返回.这个期间客户端浏览器不能干任何事. 异步: ...
- 洛谷P1546 最短网络 Agri-Net
P1546 最短网络 Agri-Net 526通过 959提交 题目提供者JOHNKRAM 标签图论贪心USACO 难度普及/提高- 提交该题 讨论 题解 记录 最新讨论 50分C++代码,求解 请指 ...
- NOIP2003 神经网络(bfs)
NOIP2003 神经网络 题目背景: 人工神经网络(Artificial Neural Network)是一种新兴的具有自我学习能力的计算系统,在模式识别.函数逼近及贷款风险评估等诸多领域有广泛的应 ...
- 7月18号day10总结
今天学习过程和小结 今天学会了用git从GitHub上克隆代码然后打包成jar包,然后在idea程序中引入这个jar包的依赖来使用jar包中的程序. 通过这个中的网址: 在Git Bash Here中 ...