LibreOJ #10002. 「一本通 1.1 例 3」喷水装置

Description:

长L米,宽W米的草坪里装有N个浇灌喷头。每个喷头都装在草坪中心线上(离两边各W/2米)。我们知道每个喷头的位置(离草坪中心线左端的距离),以及它能覆盖到的浇灌范围。请问:如果要同时浇灌整块草坪,最少需要打开多少个喷头?

Code

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int N = 25010;
struct Interval{
	double l,r;
	bool operator < (Interval &a) const {
		return l < a.l;
	}
}w[N];
int n,T,cnt;
double L,W;
void init()
{
	cnt = 0;
	memset(w,0,sizeof(w));
}
void solve()
{
	sort(w + 1,w + cnt + 1);
	int ans = 0,last = 1;
	double lb,ub = 0;
	while(ub < L)
	{
		++ans;
		lb = ub;
		for(;w[last].l <= lb && last <= cnt;++last)
		{
			if(w[last].r > ub)
			{
				ub = w[last].r;
			}
		}
		if(lb == ub && lb < L)
		{
			printf("-1\n");
			return;
		}		
	}
	printf("%d\n",ans);
}
int main()
{
	scanf("%d",&T);
	while(T--)
	{
		init();
		scanf("%d%lf%lf",&n,&L,&W);
		double pos,r;
		for(int i = 1;i <= n;++i)
		{
			scanf("%lf%lf",&pos,&r);
			if(r > (W/2))
			{
                                //勾股定理
				w[++cnt].l = pos - sqrt(r*r - (W / 2)*(W / 2));
				w[cnt].r = pos + sqrt(r*r - (W / 2)*(W / 2));
			}else{
				continue;
			}
		}
		solve();
	}
	return 0;
}
posted @ 2019-06-02 16:18  Zforw  阅读(60)  评论(0编辑  收藏  举报