码迷,mamicode.com
首页 > 其他好文 > 详细

uva 10382 Watering Grass(贪心)

时间:2015-02-03 23:11:27      阅读:448      评论:0      收藏:0      [点我收藏+]

标签:c语言   uva   

                                 uva 10382 Watering Grass


n sprinklers are installed in a horizontal strip of grass l meters long and w 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 numbers n, 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的喷头,每个喷头有对应的半径,为它的工作范围,现在要求选出最少的喷头使的这些喷头的工作范围覆盖整个草坪。

解题思路:要将圆形区域转化为矩形的有效区,t = sqrt(r * r - h * h /4)。之后就是贪心和区间规划。



#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(贪心)

标签:c语言   uva   

原文地址:http://blog.csdn.net/llx523113241/article/details/43452871

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!