UVA - 10382 Watering Grass 贪心+区间覆盖

题目大意:有一个长为L,宽为M的矩形,矩形上面有K喷水器,喷水器的圆心都和矩形宽的中心在同一条直线上,求能否使用最少的喷水器使整个矩形都被水覆盖到

解题思路:区间覆盖问题,只是要考虑到宽。喷水器的喷水半径如果小于等于矩形宽的一半的话,就不用考虑了,因为那样无法使它所在的位置有水,如果相切的话,切点两边就喷不到水了。考虑到圆与上下两条长相交,连接两个交点,作一条线段,求出圆心到该线段的中点的长度就是喷水器谁能喷到的空间,这样就能形成一个矩形,宽就可以不用考虑了,借别人图一用:

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn = 10000 + 5;
struct Node {
	double x;
	double y;
	friend bool operator <(const Node &a, const Node &b)  {
		return a.x < b.x;
	}
};
Node node[maxn];
double l,w;
int n;
int main() {
	double num1, num2;
	double temp;
	int count;
	while(scanf("%d%lf%lf",&n,&l,&w) != EOF) {
		count = 0;
		for(int i = 0; i < n; i++) {
			scanf("%lf%lf",&num1,&num2);	
			if(num2 <= w / 2)
				continue;
			else {
				temp = sqrt(num2 * num2 - w * w / 4.0);
				node[count].x = num1 - temp;
				node[count].y = num1 + temp;
				count++;
			}	
		}
	
		sort(node,node+count);
		double left = 0,right = 0;
		int i = 0,j,ans = 0;
		bool flag = true;

		while(i < count) {
			j = i;
			while(j < count && node[j].x <= left) {
				if(right <= node[j].y)
					right = node[j].y;
				j++;			
			}
			if(j == i)
				break;

			left = right;	
			ans++;
			i = j;

			if(left >= l) {
				flag = false;
				break;
			}	
		}
		if(flag)
			printf("-1\n");
		else
			printf("%d\n", ans);
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值