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, ...
随机推荐
- 剑指offer---2、二叉搜索树的后序遍历序列
剑指offer---2.二叉搜索树的后序遍历序列 一.总结 一句话总结: 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字 ...
- Linux内核知识杂记
1.内核调试手段 1.printk打印内核状态 2.产生opps时使用GDB查看调用栈 2.内核空间和用户空间区别,通信方式有哪些? Linux简化了分段机制,使得虚拟地址与线性地址总是一致,因此,L ...
- java IO 类概述表
列举常用的类方便查看,温故知新! byte input byte output character input character output Basic InputStream OutputStr ...
- Servilet初步
以http://locahost:8080/......开头,或者以/开头,都是绝对路径以路径开头:相对路径 路径/路径 Servlet执行流程:(只用自己编写执行的代码,执行的细节全是tomcat封 ...
- Zabbix监控搭建
目录 Zabbix概述 zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案 ( 基于 GPL V2 )zabbix由 2 部分构成,zabbix ...
- Nginx学习——proxy_pass
参考官网:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass 定义:用来设置被代理服务器的协议(http或https ...
- leetcode.字符串.205同构字符串-Java
1. 具体题目 给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不 ...
- [已解决]报错SyntaxError: Non-ASCII character '\xe6'
解决方案:开头加上 # -*- coding: utf-8 -*
- BigDecimal的操作工具类
import java.math.BigDecimal; /** * 进行BigDecimal对象的加减乘除,四舍五入等运算的工具类 * @author ameyume * */ public cla ...
- VS系列远程调试
其实很多时候,远程调试的话,我还是更喜欢用WinDBG的, 首先,可以练习WinDBG的使用手段, 其次,可以增加WinDBG的熟练度, 最重要的,WinDBG在内核调试部分很常用,我也很喜欢它,所以 ...