和 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. ASP.NET登录控件延伸(个性化)

    既然由登录控件涉及到了Membership,那么就不得不提到用户个性化Profile对象.个性化允许为用户保存特定的个性化信息到数据库中,因此它不同于ASP.NET状态管理之处在于可以永久性保存这些信 ...

  2. sql server 批量删除数据表

    SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Auth ...

  3. 浏览器执行js

    Scriptish chrome自带 greasemonkey http://www.firefox.net.cn/forum/viewtopic.php?f=5&t=45715

  4. H5时代的新存储简介

    1.WebStorage 分为:sessionStorage和localStorage两种,除了session的生命周期是在该域全部页面被关闭后就被清除而local是无限期存在外,二者的使用与方法属性 ...

  5. CAP定理与RDBMS的ACID

    一.分布式领域CAP理论 CAP定理指在设计分布式系统时,一致性(Consistent).可用性(Availability).可靠性(分区容忍性Partition Tolerance)三个属性不可能同 ...

  6. 大一暑假为期五周的ACM实验室培训结束了(2013.8.24)

    没想到,我的大学里第一个暑假,9周的时间只有最初的两周在家待着,接下来的7周将会在学校度过. 说真的,这是我上学以来,第一次真正好好利用的假期.在这五周里,周一.三.五下午学长都会给我们讲点知识,之后 ...

  7. 能够将 HTML 表格转换成图表的jQuery插件:Chartinator

    点这里 一个jQuery 插件能够将HTML 表格转换成图表,使用 Google Charts 实现. Chartinator当前支持以下特性: Creation of the following c ...

  8. Good Bye 2015 C. New Year and Domino 二维前缀

    C. New Year and Domino   They say "years are like dominoes, tumbling one after the other". ...

  9. pku 1182(种类并查集)

    题目链接:http://poj.org/problem?id=1182 解题思路来自discuss:http://poj.org/showmessage?message_id=152847 #incl ...

  10. Google Protocol Buffers简介

    什么是 protocol buffers ? Protocol buffers 是一种灵活.高效的序列化结构数据的自动机制--想想XML,但是它更小,更快,更简单.你只需要把你需要怎样结构化你的数据定 ...