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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值