uva 10382 Watering Grass(贪心-区间覆盖变形)
2015-02-19 23:59
337 查看
题目大意:在一个长order(目标), 宽h的草坪上有n的喷头,每个喷头有对应的半径,为它的工作范围,现在要求选出最少的喷头使的这些喷头的工作范围覆盖整个草坪。
解题思路:区间覆盖问题, 这里要注意一下每个喷头的有效作用区间并不是[p - r, p+ r], 因为圆是有弧度的,所以有效区间为t = sqrt(r * r - h * h /4), 这里要先判断r 和 h/2 的大小, [ p - t, p + t].
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
struct P {
double left;
double right;
} A[10100];
int cmp(P a, P b) {
return a.left < b.left;
}
int main() {
int n, l, w;
while (scanf("%d%d%d", &n, &l, &w) != EOF) {
for (int i = 0; i < n; i++) {
double M, R;
scanf("%lf%lf", &M, &R);
A[i].left = M - sqrt(R * R - w * w / 4.0);
A[i].right = M + sqrt(R * R - w * w / 4.0);
}
sort(A, A + n, cmp);
double cur = 0, temp = 0;
int ans = 0, i = 0;
while (cur < l) {
for (; A[i].left <= cur && i < n; i++)
temp = max(temp, A[i].right);
if (temp == cur)
break;
cur = temp;
ans++;
}
printf("%d\n", cur < l ? -1 : ans);
}
return 0;
}
解题思路:区间覆盖问题, 这里要注意一下每个喷头的有效作用区间并不是[p - r, p+ r], 因为圆是有弧度的,所以有效区间为t = sqrt(r * r - h * h /4), 这里要先判断r 和 h/2 的大小, [ p - t, p + t].
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
struct P {
double left;
double right;
} A[10100];
int cmp(P a, P b) {
return a.left < b.left;
}
int main() {
int n, l, w;
while (scanf("%d%d%d", &n, &l, &w) != EOF) {
for (int i = 0; i < n; i++) {
double M, R;
scanf("%lf%lf", &M, &R);
A[i].left = M - sqrt(R * R - w * w / 4.0);
A[i].right = M + sqrt(R * R - w * w / 4.0);
}
sort(A, A + n, cmp);
double cur = 0, temp = 0;
int ans = 0, i = 0;
while (cur < l) {
for (; A[i].left <= cur && i < n; i++)
temp = max(temp, A[i].right);
if (temp == cur)
break;
cur = temp;
ans++;
}
printf("%d\n", cur < l ? -1 : ans);
}
return 0;
}
相关文章推荐
- uva 10382 Watering Grass(贪心-区间覆盖变形)
- UVA - 10382 Watering Grass 贪心+区间覆盖
- UVA - 10382 Watering Grass (贪心+区间覆盖)
- UVA 10382 Watering Grass(贪心 区间覆盖问题)
- UVa 10382 - Watering Grass (贪心—区间覆盖)
- uva 10382 贪心 区间覆盖问题
- UVa 10382 Watering Grass (区间覆盖贪心问题+数学)
- UVA 10382 Watering Grass (贪心 + 区间覆盖问题)
- uva 10382区间覆盖贪心
- UVA 10382 贪心(区间覆盖)
- UVA - 10382 Watering Grass (区间覆盖 贪心)
- UVA10382- Watering Grass(区间覆盖问题)
- UVa 10382 - Watering Grass(贪心+区间覆盖问题)
- UVA 10382 Watering Grass 区间覆盖问题 -
- UVA 10382 - Watering Grass【贪心+区间覆盖问题+高精度】
- UVA 10020 Minimal coverage 区间覆盖问题 贪心
- UVa 10382 Watering Grass (贪心&pair使用技巧)
- UVa 10020 - Minimal coverage(区间覆盖并贪心)
- 10382 - Watering Grass(贪心 区间覆盖问题)洒水面覆盖
- UVA 10020 简单贪心区间覆盖,各种WA,记录一下