首页 > 代码库 > 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 andw meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance from the left end of the center line and its radius of operation.
What is the minimum number of sprinklers to turn on in order to water the entire strip of grass?
Input
Input consists of a number of cases. The first line for each case contains integer numbersn, l and w with n <= 10000. The next n lines contain two integers giving the position of a sprinkler and its radius of operation. (The picture above illustrates the first case from the sample input.)
Output
For each test case output the minimum number of sprinklers needed to water the entire strip of grass. If it is impossible to water the entire strip output -1.
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 Output
6
2
-1题目大意:在一个长order(目标), 宽h的草坪上有n的喷头,每个喷头有对应的半径,为它的工作范围,现在要求选出最少的喷头使的这些喷头的工作范围覆盖整个草坪。
#include<stdio.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; struct pos { double L, R; }; int cmp(pos a, pos b) { return a.L < b.L; } pos p[10005]; int main() { int n, l, w; while (scanf("%d %d %d", &n, &l, &w) == 3) { double a, b; int cnt = 0; for (int i = 0; i < n; i++) { scanf("%lf %lf", &a, &b); if (2 * b <= w) { continue; } double num = sqrt(b * b - w * w / 4.0); p[cnt].L = a - num; p[cnt++].R = a + num; } sort(p, p + cnt, cmp); int ans = 0, flag = 0; if (p[0].L > 0) { printf("-1\n"); continue; } double moveL = 0, moveR = 0; while (moveL < l) { for (int i = 0; i < n; i++) { if (p[i].L <= moveL && p[i].R > moveR) moveR = p[i].R; } if (moveL == moveR) { flag = 1; break; } moveL = moveR; ans++; } if (!flag) printf("%d\n", ans); else { printf("-1\n"); } } return 0; }
uva 10382 Watering Grass(贪心)