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, ...
随机推荐
- Android onActivityResult()运行时刻的问题
今天在开发过程中遇到一个很是怪异的问题,就是方法onActivityResult的执行问题,问题是当我从当前的Activity跳转的时候,尚未做任何动作,onActivityResult()就已经执行 ...
- MySql中创建用户,授权
第一天搞MySql好多东西都不会,幸好有网络的强大资源,首先需要注意的是任何一条sql语句都是要以分号结尾的,不然很是蛋疼的 1.新建用户. //登录MYSQL @>mysql -u root ...
- Shell基础(三):使用for循环结构、使用while循环结构、基于case分支编写脚本、使用Shell函数、中断及退出
一.使用for循环结构 目标: 本案例要求编写一个Shell脚本chkhosts.sh,利用for循环来检测多个主机的存活状态,相关要求及说明如下: 1> 对192.168.4.0/24网段执行 ...
- RVIZ可视化平台
- delphi下运行vbscript脚本
简单一个vb脚本,功能为打开被限制的注册表.Set wso = CreateObject("WScript.Shell")wso.RegWrite "HKEY_CURRE ...
- .gitignore 文件使用说明
我们在使用 Git 进行版本控制的时候,有些文件是无需纳入 Git 管理的,通常都是些自动 生成的文件,像日志或者编译过程中创建的文件.我们可以创建一个名为 .gitignore 的文件,列出要忽略的 ...
- 框架-.NET:Spring.Net
ylbtech-框架-Spring.Net:Spring.Net Spring.NET为建立企业级应用提供了一套轻量级的解决方案.通过Spring.NET,我们可以用统一且透明的方式来配置应用程序.S ...
- Hibernate 和 JPA 注解
转载请注明:Hibernate 和 JPA 注解 | 言曌博客 1.@Entity(name="EntityName") 必须, name为可选,对应数据库中一的个表 2.@Tab ...
- webpack4 入门配置研究
1. 全局安装 npm install webpack webpack-cli webpack-dev-server -g 1.1)输密文的密码(电脑开机) 1.2)安装成功 2. 输入命令mkdir ...
- 推荐20个让你学习并精通CSS的网站
1. A List Apart CSS Topics A List Apart,学习网页设计和最佳实践的首选网站.这个网站从1999年就开始发表关于CSS的文章,其中大部分文章都是面向那些更注重符合标 ...