Watering Grass (贪心,最小覆盖)
参考:
https://blog.csdn.net/shuangde800/article/details/7828675
https://www.cnblogs.com/haoabcd2010/p/6171794.html?utm_source=itdadao&utm_medium=referral


#include <iostream>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; struct node
{
double L;
double R;
}a[]; bool cmp(node a, node b)
{
return a.R > b.R;
} void sort_node(int n)
{
for (int i=;i<n;i++)
{
int k=i;
for (int j=i+;j<n;j++)
{
if (a[j].R>a[k].R)
k=j;
}
swap(a[k],a[i]);
}
} int main()
{
int n;
double length, wide; // 都定义为double类型,因为使用勾股定理的时候肯定会算出小数
double center, radius; // center为圆心,radius为半径
double t;
while(scanf("%d%lf%lf", &n, &length, &wide) != EOF)
{
for(int i = ; i < n; ++i)
{
cin >> center >> radius;
t = sqrt(radius*radius - (wide/2.0)*(wide/2.0));
a[i].L = center - t;
a[i].R = center + t;
} //sort(a, a+n, cmp); 快排不知道为什么不能AC
sort_node(n); double border = ;
int cnt = ;
while(border < length) // 此类型题目的固定套路
{
int i;
for(i = ; i < n; ++i)
{
if(a[i].L <= border && a[i].R > border)
{
border = a[i].R;
cnt++;
break;
}
} if(i == n) // 如果i==n,说明遍历完了这n个区间,仍然没有找到符合条件的,即不能完全覆盖
break;
} if(border < length)
cout << - << endl;
else
cout << cnt << endl;
} return ;
}
Watering Grass (贪心,最小覆盖)的更多相关文章
- Watering Grass(贪心)
Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long and w meters ...
- 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 贪心,水题,爆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 ...
- 10382 - Watering Grass
Problem E Watering Grass Input: standard input Output: standard output Time Limit: 3 seconds n sprin ...
- Watering Grass(贪心算法)
给定一条草坪.草坪上有n个喷水装置.草坪长l米宽w米..n个装置都有每个装置的位置和喷水半径..要求出最少需要几个喷水装置才能喷满草坪..喷水装置都是装在草坪中间一条水平线上的. n sprinkle ...
- 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选一个右端点 ...
随机推荐
- 第一周课堂笔记3th
1.bool值 True正确 False错误 数字为0时的显示 为空值时“” 2. str int bool布尔值 之间的转化 str int ...
- 玩转大数据系列之Apache Pig如何与Apache Solr集成(二)
散仙,在上篇文章中介绍了,如何使用Apache Pig与Lucene集成,还不知道的道友们,可以先看下上篇,熟悉下具体的流程. 在与Lucene集成过程中,我们发现最终还要把生成的Lucene索引,拷 ...
- Ubuntu 14.04 Ruby 2.3.3 安装
在Ubuntu 14.04通过下载Ruby源码包进行安装. 第一步,更新apt-get sudo apt-get update 通过apt-get安装ruby依赖 sudo apt-get insta ...
- 基于阿里云安装脚本扩展 之 自动安装mongodb及php扩展
好久没有发布文章了,有点跟不上当初这个博客的初衷.为了使自己的博客更新不半途而废,今天特意再写了一个自动安装脚本,一样是基于阿里云的服务端安装脚本进行的扩展.闲话不说,直接放代码: #!/bin/ba ...
- java后台使用HttpURLConnection实现百度主动推送
优点是快 不需要页面执行,,发布文章之后立即推送,所以,不管有没有人访问,都可以自动实时推送 尝试了一下httpclient,没找到相关资料,post方式无法塞url进去 最后改为 import ja ...
- JS基础之EL表达式
一.EL表达式简介 EL 全名为Expression Language.EL主要作用: 1.获取数据 EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域 中检索java对象.获取数 ...
- Python - 集合与元素之数据类型和变量总结
变量 变量的作用:保存状态(程序的运行本质是一系列的变化,变量的目的就是用来保存状态,变量值的变化就构成了程序运行的不同结果.) 例如:cs枪战中,一个人的生命可以表示为life = True 表示存 ...
- jqGrid 属性、事件全集
<html> ... <table id="list1"></table> <div id="pager1">& ...
- springboot拦截器之验证登录
添加jar包,这个jar包不是必须的,只是在拦截器里用到了,如果不用的话,完全可以不引入 <dependency> <groupId>org.apache.commons< ...
- 2019-7-15-win10-uwp-在笔迹开始书写拿到书写移动事件
title author date CreateTime categories win10 uwp 在笔迹开始书写拿到书写移动事件 lindexi 2019-7-15 8:58:5 +0800 201 ...