您的位置:首页 > 其它

Uva 10382 (区间覆盖) Watering Grass

2014-09-01 17:33 453 查看
和 Uva 10020几乎是一样的,不过这里要把圆形区域转化为能够覆盖的长条形区域(一个小小的勾股定理)

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

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

//#define LOCAL
#include <vector>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <functional>
using namespace std;

const int MAXN = 10240;
const double EPS = 1e-11;

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) == 3)
{
lenth = (double)l;
width = w / 2.0;
ranges.clear();

for(int i = 0; i < n; ++i)
{
int position, radius;
double xw;
Range range;

scanf("%d%d", &position, &radius);
if(radius * 2 <= 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 = 0;
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 = -1;
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 = -1;
printf("%d\n", minCover);
}
return 0;
}


代码君
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: