GZHU18级寒假训练:Virgo's Trial-J

UVA-10382
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


不确定这题算不算DP,但是是要一个数字记录被覆盖到的草地,若果所有圆都覆盖不了,就输出不可能。


#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
using namespace std;
 
const int N = 10005;
int n, l, w;
int visit[N];
 
double Max (const double a, const double b) { return a > b ? a: b; }
 
struct Len {
	
	double L, R;	
}len[N];
 
int cmp (const Len &a, const Len &b) { return a.L < b.L;}
 
void handle (int c, int r, int i) {
	
	double r1 = r;
	double c1 = c;
	double w1 = w;
	double temp = sqrt (r1 * r1 - 0.25 * w1 * w1);
	len[i].L = c - temp;
	len[i].R = c + temp;
}
 
int solve () {
	
	memset (visit, 0, sizeof (visit));
	if (len[0].L > 0)
		return -1;
	double s = 0;
	double ll = -1;
	int m = -1;
	for (int i = 0; i < n; i++) {
		
		if (len[i].L <= s) {
			
			if (ll < len[i].R) {
 
				visit[i] = 1;
				if (m >= 0)
					visit[m] = 0;
				m = i;
				ll = Max (ll, len[i].R);
			}
		} else {
			
			if (s == ll)
				return -1;
			s = ll;
			i--;
			m = -1;
		}
//		printf ("%f\n", ll);
	}
//	printf ("%f\n", ll);
	if (ll < l)
		return -1;
	int count = 0;
	for (int i = 0; i < n; i++)
		if (visit[i])
			count++;
	return count;
}
 
int main () {
	
	int c, r;
	while (scanf ("%d%d%d", &n, &l, &w) != EOF) {
 
		for (int i = 0; i < n; i++) {
 
			scanf ("%d%d", &c, &r);
			handle (c, r, i);
		}
		sort (len, len + n, cmp);
		
/*		for (int i = 0; i < n; i++)
			printf ("%f %f\n", len[i].L, len[i].R);*/
		printf ("%d\n", solve());
 
	}
	return 0;
}



代码来源:https://blog.csdn.net/u012997373/article/details/38150585

猜你喜欢

转载自blog.csdn.net/weixin_43316754/article/details/86688328
今日推荐