uva 10382 Watering Grass_贪心

题意:给你个矩形n*m,再给你n个圆的圆心坐标和半径,问最用最少用几个圆把这个矩形覆盖

思路:直接想发现这问题不容易,后来发现可以把圆看做区间(能把矩形面积覆盖),然后这个问题就容易解决了

#include <iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 10100
struct node{
	double l,r;
}p[N]; 
int main(int argc, char** argv) {
	int n,i,ans;
	double width,heigh,x,y,u;
	while(scanf("%d%lf%lf",&n,&width,&heigh)!=EOF){
		heigh/=2.0;
		for(i=0;i<n;i++){
			scanf("%lf%lf",&x,&y);
			if(y>=heigh){
				y=sqrt(y*y-heigh*heigh);
				p[i].l=x-y;
				p[i].r=x+y;
			}
		}
		double start=0,end=width,maxr;
		ans=0;
		while(start<end){
			maxr=0;
			for(i=0;i<n;i++){
				if(p[i].l<=start&&p[i].r>maxr)
					maxr=p[i].r;
			}
			if(start==maxr){
				ans=-1;
				break;
			}
			ans++;
			start=maxr;
		}
		printf("%d\n",ans);
	}
	return 0;
}


posted @ 2014-02-28 22:54  Teemo的技术blog  阅读(123)  评论(0编辑  收藏  举报