和 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. Sqli-labs less 63

    Less-63 和less62一致,我们只需要看到sql语句上 $sql="SELECT * FROM security.users WHERE id='$id' LIMIT 0,1&quo ...

  2. Ubuntu下开启ssh服务

    网上有很多介绍在Ubuntu下开启SSH服务的文章,但大多数介绍的方法测试后都不太理想,均不能实现远程登录到Ubuntu上,最后分析原因是都没有真正开启ssh-server服务.最终成功的方法如下: ...

  3. POJ 2226

    Muddy Fields Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7557   Accepted: 2791 Desc ...

  4. POJ 3336 Count the string (KMP+DP,好题)

    参考连接: KMP+DP: http://www.cnblogs.com/yuelingzhi/archive/2011/08/03/2126346.html 另外给出一个没用dp做的:http:// ...

  5. Ehcache使用

    http://www.360doc.com/content/14/0423/17/16946725_371472946.shtml http://www.myexception.cn/web-appl ...

  6. 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”

    VS2012启动/加载项目出问题 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage, Microsoft.Visual ...

  7. 使用RockMongo管理MongoDB

    http://blog.csdn.net/mydeman/article/details/7082730

  8. Android 核心分析 之七Service深入分析

    Service深入分析 上一章我们分析了Android IPC架构,知道了Android服务构建的一些基本理念和原理,本章我们将深入分析Android的服务.Android体系架构中三种意义上服务: ...

  9. android中使用html作布局文件

    在android开发中,通常使用xml格式来描述布局文件.就目前而言,熟悉android布局及美化的人员少之又少,出现了严重的断层.大部分企业,其实还是程序员自己动手布局.这样既浪费时间和精力,也未必 ...

  10. 忘记导入struts2-xxx-plugin-x.x.x.jar导致服务器启动报Unable to load configuration.Caused by: Parent package is not defined: xxx-default

    今天做的一个Struts2+MyFaces(JSF)+Spring的应用,为了使用JSF,我的struts.xml中使用了如下代码 <package name="jsf" e ...