[贪心]UVA10382 Watering Grass

本文介绍了一道关于在指定范围内使用最少数量的喷水器来覆盖整个草坪的问题,并提供了一个具体的解决方案。通过将圆形喷洒范围转换为水平条带上的矩形范围,采用贪心算法策略来确定最少所需的喷水器数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem E
Watering Grass
Input:
 standard input
Output: standard output
Time Limit: 3 seconds

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 nl 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


(Regionals 2002 Warm-up Contest, Problem setter: Piotr Rudnicku)

 


题意:有一个草坪,在草坪中有一些喷水装置,给出这个草坪的长和宽,和喷水装置的位置和半径,求最少放置多少个喷水装置能使得草坪被全部覆盖?

思路:题目一看,就知道是区间覆盖问题,用贪心解决,因为是圆形区域覆盖不好计算,我们把圆形区域转换到草坪上来,就变成了矩形区域,这样就比较好计算了。

#include<iostream>
#include<algorithm>
#include<cmath>

using namespace std;

class Circle
{
public:
	double left,right;
}circle[10005];


int main()
{
	int num;
	double lenth,width;
	while(cin>>num>>lenth>>width)
		{
			int i,j,k=0;
			double pos,radius;
			for(i=0;i<num;i++)
				{
					cin>>pos>>radius;
					if(radius*2>=width)
						{
							double l,r;
							l=pos-sqrt(radius*radius-(width/2)*(width/2));
							r=pos+sqrt(radius*radius-(width/2)*(width/2));
							circle[k].left=l;
							circle[k++].right=r;
						}
				}
			double begin=0,end=lenth;
			double maxlen=0;
			int cnt=0;
			while(begin<end)
				{
					maxlen=0;
					for(i=0;i<k;i++)
						{
							if(circle[i].left<=begin&&circle[i].right>maxlen)
								{
									maxlen=circle[i].right;
								}
						}
					if(maxlen==begin)
						{
							cnt=-1;
							break;
						}
					cnt++;
					begin=maxlen;
				}
			cout<<cnt<<endl;
		}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值