UVA 10382 Watering Grass 贪心+区间覆盖问题
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’.

题意:给你n个喷水装置,给出其长度和宽度,接下去给出其中心和半径,问最少需要多少个喷水装置能够覆盖整条草条。
思路:利用勾股定理求出每个装置能够到达的左区间端点和右区间端点并存在结构体中,进行排序(左区间从小到大,右区间从大到小),最后转化成区间覆盖问题即可。
特判:
- 排序完成后的第一个区间的左端点需要小于等于0,最大的右端点需要大于等于草条的长度
- 需要考虑下一个区间的左端点小于等于当前区间的右端点,但是其右端点也小于等于该点的右区间
- 利用flag进行标记,若在遍历的过程中出现中间断开,也就是说下一个区间的左端点比当前的最大右区间还要大
考虑到7-15、13-17、15-20的这种情况,很容易把第二个算进去,本来通过变量去记录最长距离,然后去更新变量的距离和坐标,通过for循环结束,但是发现这样做右可能在测试数据上过不去,而随时随地能够终止循环的需要利用while循环来写。
已AC:
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std; struct node
{
double l;
double r;
} a[]; int cmp1(node x,node y)
{
if(x.l!=y.l)
return x.l<y.l;
else
return x.r>y.r; }
//按左边界的坐标点从小到大排序,//右端点为什么不需要排序 int main()
{
std::ios::sync_with_stdio(false);
int n;
double len,w;
double c,rr;
while(cin>>n>>len>>w)
{
int num=;
double maxxr=-1.0;
for(int i=; i<n; i++)
{
cin>>c>>rr;
if(rr*<=w)
{
continue;
}
else
{
double l=c-sqrt(rr*rr-w*w/);
double r=c+sqrt(rr*rr-w*w/);
// printf("%lf--%lf\n",l,r);
if(r>=maxxr)
{
maxxr=max(maxxr,r);
}
// if(l<=0)
// l=0;
a[num].l=l;
a[num++].r=r;
}
}
sort(a,a+num,cmp1);
int k=;
if(a[].l>||maxxr<len)
{
cout<<-<<endl;
continue;
}
double maxx=;
int ans=;
int flag=;
int ww=;
while(maxx<len)
{
double uu=maxx;
for(int i=; i<num; i++)
{
if(a[i].l<=uu&&a[i].r>maxx)
{
// minn=a[i].l;
maxx=a[i].r;
// zz=i;
}
}
// printf("%lf----%d\n",maxx,zz);
if(uu==maxx&&uu<len)
{
ww=;break;
}
//minn=a[zz].l;
ans++;
/*for(int i=0; i<num; i++)
{
//printf("%lf*****%lf\n",a[i].l,a[i].r);
if(a[i].l<=maxx)
{
if(a[i].r>maxx)
{
maxx=a[i].r;
ans++;
printf("%lf----%lf\n",a[i].l,a[i].r);
}
else
{
continue;
}
}
if(a[i].l>maxx)
{
flag=0;
break;
}
if(a[i].r>=len)
{
flag=1;
break;
}*/
//}//中间部分要是连接不上,处理:进行flag标记
}
// printf("%d\n",ans);
if(ww==)
cout<<ans<<endl;
else
cout<<-<<endl;
}
return ;
}
利用for循环的代码,代码是错误的,但有这个思路在
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std; struct node
{
double l;
double r;
} a[]; int cmp1(node x,node y)
{
if(x.l!=y.l)
return x.l<y.l;
else
return x.r>y.r; }
//按左边界的坐标点从小到大排序 int main()
{
//std::ios::sync_with_stdio(false);
int n;
double len,w;
double c,rr;
while(cin>>n>>len>>w)
{
int num=;
double maxxr=-1.0;
for(int i=; i<n; i++)
{
cin>>c>>rr;
if(rr*<=w)
{
continue;
}
else
{
double l=c-sqrt(rr*rr-w*w/);
double r=c+sqrt(rr*rr-w*w/);
// printf("%lf--%lf\n",l,r);
if(r>=maxxr)
{
maxxr=max(maxxr,r);
}
// if(l<=0)
// l=0;
a[num].l=l;
a[num++].r=r;
}
}
sort(a,a+num,cmp1);
int k=;
int pp=;
if(a[].l>||maxxr<len)
{
cout<<-<<endl;
continue;
}
double maxx=;
double minn=;
int ans=;
int flag=;
int w=;
int ww=;
int d=;
int z=;
int qq=;
for(int i=; i<num; i=qq)
{
//printf("%lf*****%lf\n",a[i].l,a[i].r);
if(a[i].l<=maxx)
{
if(a[i].r>maxx)
{
if(maxx>=len)
{
printf("%d\n",ans);
pp=;
}
w=i;
ww=a[i].r-a[i].l;
// maxx=a[i].r;
// ans++;
// printf("%lf----%lf\n",a[i].l,a[i].r);
for(int j=i+;j<num;j++)
{
z=a[j].r-a[j].l;
if(a[j].l<=maxx&&a[j].r>maxx)
{
if(z>ww)
{
w=z;
}
}
if(a[j].l>maxx)
break;
}
// printf("%lf----%lf\n",a[i].l,a[i].r);
maxx=a[w].r;
qq=w;
printf("%lf\n",maxx);
w=;
ans++;
if(maxx>=len)
{
printf("%d\n",ans);
pp=;
}
printf("%d\n",ans);
if(pp==)
break;
}
else
{
continue;
}
if(pp==)
break;
} // if(a[i].l>maxx)
// {
// flag=0;
// break;
// }
// if(a[i].r>=len)
// {
// flag=1;
// break;
// }
}
// printf("%d\n",ans); // if(flag)
/*if(maxx>=len)
cout<<ans<<endl;
*/
if(pp==)
cout<<-<<endl;
}
return ;
}
UVA 10382 Watering Grass 贪心+区间覆盖问题的更多相关文章
- 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(区间覆盖)
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 贪心,水题,爆int 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 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的草坪,在草坪上有n个洒水器,给出洒水器的位置和洒水半径,求能浇灌全部草坪范围的洒水器的最小个数. 经典贪心问题:区间覆盖.用计算几何对洒水器的覆盖范围简单处理一下即可得 ...
- UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)
Minimal coverage The Problem Given several segments of line (int the X axis) with coordinates [Li, ...
随机推荐
- obj.offsetHeight与obj.style.height $(obj).height()与$(obj).css('height')
相同:都可以获取obj的高度区别:(1)obj.offsetHeight可以获取外部.内嵌和内联中定义的高,而obj.style.height只能获取内联中定义的高:(2)obj.offsetHeig ...
- 如何使用android-support-V7包中ActionBar(Eclipse版)
$*********************************************************************************************$ 博主推荐 ...
- js 获取数组中的最大值和最小值
var arr = [3,12,23,18,25,33,22,30,1] 方案一: 思想 首先对数组进行排序(小 >大),第一项为最小值,最后一项为最大值 var min; var max; a ...
- android ellipsize的使用及实现跑马灯效果总结
参考资料: http://blog.csdn.net/huiwolf2008/article/details/7901084 http://www.cnblogs.com/Gaojiecai/arch ...
- CSS:CSS 属性 选择器
ylbtech-CSS:CSS 属性 选择器 1.返回顶部 1. CSS 属性 选择器 具有特定属性的HTML元素样式 具有特定属性的HTML元素样式不仅仅是class和id. 注意:IE7和IE8需 ...
- springBoot使用PageHelper当超过最大页数后仍然返回数据
在SpringBoot中使用PageHelper分页插件时,如果设置pagehelper.reasonable=true时,pageNum<=0 时会查询第一页, pageNum>page ...
- jmeter 创建接口测试案例
1 怎么做接口测试? 一般情况下,由于我们项目前后调用主要是基于http协议的接口,所以测试接口时主要是通过工具或代码模拟http请求的发送和接收.所以我们下面整理了一下使用Jmeter工具进行htt ...
- 如何使用Intellij IDEA工具导入SVN项目
Intellij IDEA是目前主流的IDE开发工具,工程项目导入也是必不可少的操作,本文讲述如何用 IDEA工具导入SVN项目. 步骤一:选择VCS打开Intellij IDEA开发工具,在导航栏中 ...
- 拾遗:~/.zshrc 配置
Tips: zsh 默认仅显示最近 16 条历史记录 $ # 等价于 history - :显示最近 条记录 $ history $ # 等价于 history - : 显示从第 条到最后 条,即是全 ...
- 前端(六)—— 伪类选择器:a标签的伪类、内容伪类、索引伪类、取反伪类
a标签的伪类.内容伪类.索引伪类.取反伪类 一.a标签的四大伪类 :link:未访问状态 :hover:悬浮状态 :active:活跃状态 :visited:已访问状态 四大伪类也可用于其他标签 &l ...