Uva 10382 (区间覆盖) Watering Grass
和 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的更多相关文章
- 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(区间覆盖)
n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...
- 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(区间覆盖,贪心)题解
题意:有一块草坪,这块草坪长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(区域覆盖问题)
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
题目大意:有一条长为l,宽为w的草坪,在草坪上有n个洒水器,给出洒水器的位置和洒水半径,求能浇灌全部草坪范围的洒水器的最小个数. 经典贪心问题:区间覆盖.用计算几何对洒水器的覆盖范围简单处理一下即可得 ...
- UVA 10382.Watering Grass-贪心
10382 - Watering Grass Time limit: 3.000 seconds n sprinklers are installed in a horizontal strip of ...
随机推荐
- JavaScript中this的工作原理以及注意事项
在JavaScript中,this 的概念比较复杂.除了在面向对象编程中,this 还是随处可用的.这篇文章介绍了this 的工作原理,它会造成什么样的问题以及this 的相关例子. 要根据this ...
- Level2行情和传统行情的区别
序号 Level2行情 传统行情 Level 2特点 Level 2行情优势 1 每3秒钟发送一次行情信息 每6秒钟发送一次 行情显示速度更快 投资者更及时地获得交易信息 2 证券逐笔成交明细信息 证 ...
- 利用MariaDB Galera Cluster实现mariadb的多主复制
一.MariaDB Galera Cluster概要: .简述: MariaDB Galera Cluster 是一套在mysql innodb存储引擎上面实现multi-master及数据实时同步的 ...
- ZOJ3560 Re:the Princess(高斯消元法)
题目要读很久才能理解它的意思和笑点(如果你也看过那个笑话的话),读懂之后就会发现是一个高斯消元法的题目,对于我来说难点不在高斯消元,而在于字符串处理.先来说说题意吧: 总共有n个人,n个人都会有一段话 ...
- 只 一行显示可左右滚动的文本(UITextField中文限制)
// // ViewController.m // 一行显示可滚动的文本 // // Created by apple on 15-5-8. // Copyright (c) 2015年 apple. ...
- $q -- AngularJS中的服务
此 承诺/延迟(promise/deferred)实现 的灵感来自于 Kris Kowal's QCommonJS Promise建议文档 将承诺(promise) 作为和 异步执行操作(action ...
- Nutch配置:nutch-default.xml详解
/×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...
- hdu 4618(最大回文子矩阵)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4618 昨天多校的一道题,说多了都是泪啊,为了一道图论题,磨了那么久,结果是别的题都没看,没办法,补呗. ...
- jmeter 异步子请求测试随笔
好久没写技术类的博客了,都不知道自己都在忙啥.... 最近陆续遇到了一些异步子请求的测试需求,比如打开某一个页面A,A页面里的js会再调用B,C,D,E等请求,针对这个页面的测试,我最近做了一些思考: ...
- 欧拉工程第53题:Combinatoric selections
package projecteuler51to60; class p53{ void solve1(){ int count=0; int Max=1000000; int[][] table=ne ...