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个喷水装置,给出其长度和宽度,接下去给出其中心和半径,问最少需要多少个喷水装置能够覆盖整条草条。

思路:利用勾股定理求出每个装置能够到达的左区间端点和右区间端点并存在结构体中,进行排序(左区间从小到大,右区间从大到小),最后转化成区间覆盖问题即可。

特判:

  1. 排序完成后的第一个区间的左端点需要小于等于0,最大的右端点需要大于等于草条的长度
  2. 需要考虑下一个区间的左端点小于等于当前区间的右端点,但是其右端点也小于等于该点的右区间
  3. 利用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 贪心+区间覆盖问题的更多相关文章

  1. UVA 10382 Watering Grass(区间覆盖,贪心)题解

    题意:有一块草坪,这块草坪长l 米,宽 w 米,草坪有一些喷头,每个喷头在横坐标为 p 处,每个喷头的纵坐标都是(w/2) ,并且喷头的洒水范围是一个以喷头为圆心,半径为 r 米的圆.每次最少需要打开 ...

  2. UVa 10382 Watering Grass (区间覆盖贪心问题+数学)

    题意:有一块长为l,宽为w的草地,在其中心线有n个喷水装置,每个装置可喷出以p为中心以r为半径的圆, 选择尽量少的装置,把草地全部润湿. 析:我个去啊,做的真恶心,看起来很简单,实际上有n多个坑啊,首 ...

  3. UVA 10382 Watering Grass (区间覆盖,贪心)

    问题可以转化为草坪的边界被完全覆盖.这样一个圆形就换成一条线段. 贪心,从中选尽量少的线段把区间覆盖,按照把线段按左端点排序,记录一个当前已经覆盖区间的位置cur, 从左端点小于等于cur选一个右端点 ...

  4. UVA 10382 Watering Grass(区间覆盖)

    n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...

  5. 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 ...

  6. UVa 10382 - Watering Grass 贪心,水题,爆int 难度: 0

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...

  7. UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】

    UVa 10382 - Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long a ...

  8. UVa 10382 - Watering Grass

    题目大意:有一条长为l,宽为w的草坪,在草坪上有n个洒水器,给出洒水器的位置和洒水半径,求能浇灌全部草坪范围的洒水器的最小个数. 经典贪心问题:区间覆盖.用计算几何对洒水器的覆盖范围简单处理一下即可得 ...

  9. UVA 10020 Minimal coverage(贪心 + 区间覆盖问题)

     Minimal coverage  The Problem Given several segments of line (int the X axis) with coordinates [Li, ...

随机推荐

  1. Delphi内建异常类 异常处理参考

    标签: delphiexceptionwindowscasting编程integer 2012-05-19 12:53 2579人阅读 评论(0) 收藏 举报 分类: Delphi(96) [详细过程 ...

  2. P1523 旅行商简化版

    P1523 旅行商简化版 题目背景 欧几里德旅行商(Euclidean Traveling Salesman)问题也就是货郎担问题一直是困扰全世界数学家.计算机学家的著名问题.现有的算法都没有办法在确 ...

  3. Vi/Vim查找,替换,统计使用方法

    Vi/Vim查找替换使用方法 vi/vim 中可以使用 :s 命令来替换字符串.该命令有很多种不同细节使用方法,可以实现复杂的功能,记录几种在此,方便以后查询. 可以使用 # 作为分隔符,此时中间出现 ...

  4. HTML-参考手册: HTTP 方法:GET 对比 POST

    ylbtech-HTML-参考手册: HTTP 方法:GET 对比 POST 1.返回顶部 1. HTTP 方法:GET 对比 POST 两种最常用的 HTTP 方法是:GET 和 POST. 什么是 ...

  5. Codeforces451A-Game With Sticks-思维

    After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are pl ...

  6. Python 操作excel常见异常

    一.使用xlrd模块读取excel: 1.报错:IndexError: list index out of range,如下图 解决方法:reading_sheet.cell(1,0).value中c ...

  7. python3 使用int函数将浮点数转换成整数

    int函数将浮点数转换成整数需要注意的地方 >>> int(153)153>>> int(153.4)153>>> int(153.5)153&g ...

  8. JUC源码分析-集合篇(一)ConcurrentHashMap

    JUC源码分析-集合篇(一)ConcurrentHashMap 1. 概述 <HashMap 源码详细分析(JDK1.8)>:https://segmentfault.com/a/1190 ...

  9. SaaS,PaaS,IaaS都是什么鬼?

    IaaS Infrastructure as a Service,基础设施即服务. 假如你现在要做一个网站,你肯定要有一台服务器或者虚拟机,要么自己搭建,要么买服务器运营商的.说白了,IaaS就是解决 ...

  10. redis List相关命令