UVA-10382 Watering Grass

题意:有一个长方形草坪,长为l,宽为w,在它的水平中心线上可以安装n个喷水器来为草坪浇水,每个喷水器能够灌溉的范围是以该喷水器为中心的半径为r的圆。我们要灌溉整个草坪,问你最少需要的喷水器数。

题解:很明显这是一个最小区间覆盖问题~~(老长时间不写了,写个blog记录下)~~

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
struct node{
	double left;
	double right;
}a[10009];
double solve(double r,double w)
{
	double tmp=r*r-w*w/4;
	return sqrt(tmp);
}
bool cmp(node x,node y)
{
	return x.right>y.right;
}
int main()
{
	int n,cnt,ans;
	double x,r,l,w,en;
	while(~scanf("%d%lf%lf",&n,&l,&w))
	{
		cnt=0;ans=0;en=0;
		for(int i=1;i<=n;i++)
		{
			cin>>x>>r;
			if(2*r<w) continue;
			double tmp;
			tmp=solve(r,w);
			a[++cnt].left=x-tmp;
			a[cnt].right=x+tmp;
		}
		sort(a+1,a+1+cnt,cmp);
		while(en<l)
		{
			int i;
			for(i=1;i<=cnt;i++)
			{
				if(a[i].left<=en&&a[i].right>en)
				{
					en=a[i].right;
					ans++;
					break;
				}
			}
			if(i==cnt+1) break;
		}
		if(en<l)
			cout<<"-1"<<endl;
		else
			cout<<ans<<endl;
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值