Uva 10382 (区间覆盖) Watering Grass
2014-09-01 17:33
453 查看
和 Uva 10020几乎是一样的,不过这里要把圆形区域转化为能够覆盖的长条形区域(一个小小的勾股定理)
学习一下别人的代码,练习使用STL的vector容器
这里有个小技巧,用一个微小量EPS来弥补浮点运算中的误差
代码君
学习一下别人的代码,练习使用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; }
代码君
相关文章推荐
- UVa 10382 Watering Grass (区间覆盖贪心问题+数学)
- UVa 10382 - Watering Grass(贪心+区间覆盖问题)
- UVA 10382 Watering Grass (贪心 + 区间覆盖问题)
- UVa 10382 - Watering Grass (贪心—区间覆盖)
- UVA - 10382 Watering Grass (区间覆盖 贪心)
- UVa 10382 - Watering Grass(贪心算法,区间覆盖问题)
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
- UVa 10382 Watering Grass 解题报告(贪心 最小覆盖)
- uva 10382 - Watering Grass(区域覆盖问题)
- 10382 - Watering Grass(贪心 区间覆盖问题)洒水面覆盖
- uva 10382 Watering Grass(贪心-区间覆盖变形)
- uva 10382 贪心 区间覆盖问题
- UVA 10382 Watering Grass
- UVA 10382 Watering Grass
- UVA - 10382 Watering Grass 贪心+区间覆盖
- UVa 10382 Watering Grass
- UVA10382-Watering Grass-贪心 NYOJ6-喷水装置(一)-贪心
- uva 10382 Watering Grass(贪心-区间覆盖变形)
- UVa 10382 - Watering Grass
- uva 10382 Watering Grass