和 Uva 10020几乎是一样的,不过这里要把圆形区域转化为能够覆盖的长条形区域(一个小小的勾股定理)

学习一下别人的代码,练习使用STL的vector容器

这里有个小技巧,用一个微小量EPS来弥补浮点运算中的误差

 //#define LOCAL
#include <vector>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <functional>
using namespace std; const int MAXN = ;
const double EPS = 1e-; struct Range
{
double a, b;
inline bool operator< (const Range& rhs) const
{
return a < rhs.a || (a == rhs.a && b < rhs.b);
}
}; int main(void)
{
#ifdef LOCAL
freopen("10382in.txt", "r", stdin);
#endif int n, l, w;
double lenth, width;
vector<Range> ranges;
ranges.reserve(MAXN);//reserve()函数提前设定容量大小,避免多次容量扩充操作导致效率低下 while(scanf("%d%d%d", &n, &l, &w) == )
{
lenth = (double)l;
width = w / 2.0;
ranges.clear(); for(int i = ; i < n; ++i)
{
int position, radius;
double xw;
Range range; scanf("%d%d", &position, &radius);
if(radius * <= w) continue;
xw = sqrt((double)radius * radius - width * width); range.a = position - xw;
if(range.a > lenth + EPS) continue;
else if(range.a - EPS < 0.0) range.a = 0.0;
range.b = position + xw;
ranges.push_back(range);
} sort(ranges.begin(), ranges.end()); int minCover = ;
double start = 0.0, end = 0.0;
for(vector<Range>::iterator pr = ranges.begin(); pr != ranges.end(); )
{
start = end;
if(pr->a > start + EPS)
{
minCover = -;
break;
}
++minCover;
while(pr != ranges.end() && pr->a <= start)
{
if(pr->b > end + EPS) end = pr->b;
++pr;
}
if(end > lenth + EPS)
break;
}
if(end + EPS < lenth) minCover = -;
printf("%d\n", minCover);
}
return ;
}

代码君

Uva 10382 (区间覆盖) Watering Grass的更多相关文章

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

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

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

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

  3. UVA 10382 Watering Grass 贪心+区间覆盖问题

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

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

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

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

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

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

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

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

  8. UVa 10382 - Watering Grass

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

  9. UVA 10382.Watering Grass-贪心

    10382 - Watering Grass Time limit: 3.000 seconds n sprinklers are installed in a horizontal strip of ...

随机推荐

  1. jQuery scroll事件

    scroll事件适用于window对象,但也可滚动iframe框架与CSS overflow属性设置为scroll的元素. $(document).ready(function () { //本人习惯 ...

  2. DF学Mysql(三)——索引操作

    概要: 数据库对象索引其实与书的目录非常相似,主要是为了提高从表中检索数据的速度. 由于数据存储在数据库表中,所以索引是创建在数据库表对象上的,由表中的一个字段或多个字段生成的键组成,这些键存储在数据 ...

  3. Difference Between Vector and Deque in C++

    1) Dequeue can quickly insert or delete both at the front or the end. However, vector can only quick ...

  4. springMVC视频教程

    http://edu.51cto.com/index.php?do=lession&id=42165

  5. 15 things to talk about in a healthy relationship

    15 things to talk about in a healthy relationship男女交往中可以谈论的15个话题 1. Your Daily Activities 1. 你的日常活动 ...

  6. 最简单的jdbc程序

    package cn.ytu.mybatis.jdbc;   import java.sql.Connection; import java.sql.DriverManager; import jav ...

  7. iOS开发--自动布局

    距离左边的: 距离顶部的: 距离右边的: 距离底部的:

  8. MSChart 控件

    微软发布了.NET 3.5框架下的图表控件,功能很强劲,基本上能想到的图表都可以使用它绘制出来,给图形统计和报表图形显示提供了很好的解决办法,同时支持Web和WinForm两种方式,不过缺点也比较明显 ...

  9. Android SQLite数据库增删改查操作

    一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库——SQLite,SQLite3支持NULL.INTEGER.REAL(浮点数字). TEXT(字符 ...

  10. sublime3配置Quick-X+自动错误提示

    sublime3配置 安装Package Control 配置Quick-x API提示 配置Lua自动语法错误提示 sublime3 安装 Package Control View->Show ...