uva 10382题解(贪心)

题目大意:
有一块草坪,长为l,宽为w,在它的水平中心线上有n个位置可以安装喷水装置,各个位置上的喷水装置的覆盖范围为以它们自己的半径ri为圆。求出最少需要的喷水装置个数。

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

这题可以转化成区间覆盖问题,简单贪心
思路见码
#include<cstdio>  
#include<cstring>  
#include<cmath>  
#include<cstdlib>  
#include<iostream>  
#include<algorithm>  
#include<vector>  
#include<map>  
#include<queue>  
#include<stack> 
#include<string>
#include<map> 
using namespace std;  
#define LL long long  
const int maxn=10005;
struct Pen{
	double lb,rb;
	bool operator < (const Pen& A) const {
		return lb<A.lb;
	}
}pens[maxn];
int main(){
	//freopen("input.txt","r",stdin);
	int n;
	double l,w;
	while(scanf("%d%lf%lf",&n,&l,&w)!=EOF){
		for(int i=0;i<n;i++){
			double p,r;
			scanf("%lf%lf",&p,&r);
			pens[i].lb=p-sqrt(r*r-(w*w)/4);
			pens[i].rb=p+sqrt(r*r-(w*w)/4);
		}
		sort(pens,pens+n);
		double lpos=0,rmax=0,newpos=0;
		int ans=0,ifpossible=1;
		for(int i=0;i<n;i++){
			Pen& d=pens[i];
			if(d.lb>lpos){
				lpos=newpos;
				if(d.lb>lpos){
					ifpossible=0;
					break;
				}
				ans++;
			}
			if(d.rb>rmax){
				newpos=d.rb;
				rmax=d.rb;
				if(rmax>=l){
					ans++;
					break;
				}
			}
		}
		if(pens[0].lb>0||rmax<l) ifpossible=0;
		if(ifpossible) printf("%d\n",ans);
		else printf("-1\n");
	}
	return 0;
} 

 



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值