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, ...
随机推荐
- bzoj1001 [ICPC-Beijing 2006]狼抓兔子
我满心以为本题正解为最短路,结果到处都是最大流…… 几乎所有的都写了什么“对偶图”跑最短路,但我真的不知道什么叫做对偶图---------------------------------------- ...
- AngularJS 指令实践指南(一)
指令(Directives)是所有AngularJS应用最重要的部分.尽管AngularJS已经提供了非常丰富的指令,但还是经常需要创建应用特定的指令.这篇教程会为你讲述如何自定义指令,以及介绍如何在 ...
- ASP.NET Core学习——2
Application Startup ASP.NET Core为应用程序提供了处理每个请求的完整控制.Startup类是应用程程的入口(entry point),这个类可以设置配置(configur ...
- arttemplate02
1.后台传来的数据 { "code": 200, "checkRecords": [ { "id": "402881e75cc80 ...
- 【转】/bin/bash^M: bad interpreter: 没有那个文件或目录
运行脚本时出现了这样一个错误,打开之后并没有找到所谓的^M,查了之后才知道原来是文件格式的问题,也就是linux和windows之间的不完全兼容,解决的方法有两种: 1 如果os中已经安装dos2un ...
- 四、vim 编辑器
vim编辑器 -rw-r--r--. 1 root root 1982 8月 2 2017 /etc/virc vi 配置文件 -rw-r--r--. 1 root root 1982 8月 2 20 ...
- mysql shell脚本
mysql shell连接脚本 本地连接及远程链接 #!/bin/bash #连接MySQL数据库 Host=127.0.0.1 User=username PASSWORD=password POR ...
- Java高级进阶:自定义ClassLoader
假如我们的类不在classpath下,而我们又想读取一个自定义的目录下的class,如果做呢? 读取自定义目录的类 示例读取c:/test/com/test.jdk/Key.class这个类. pac ...
- DNF邀请码开发再开发方案需求
一.原因分析: 1.现实原因:主播粉丝量级有限,一定规模粉丝注册消耗完后无法进 行之后合作 2.主播资源有限,能合作主播数量少 3.直播粉丝真实接近核心用户,但是不能将其有效转化为平台流水 ...
- java-day22
数据库设计的范式 * 概念:设计数据库时,需要遵循的一些规范.要遵循后边的范式要求,必须先遵循前边的所有范式要求 设计关系数据库时,遵从不同的规范要求,设计出合理的关系型数据库,这些不 ...