【Loj #10002. 「一本通 1.1 例 3」喷水装置】题解

题目链接

题目

长 LLL 米,宽 WWW 米的草坪里装有 nnn 个浇灌喷头。每个喷头都装在草坪中心线上(离两边各 W2frac{W}{2}​2​​W​​ 米)。我们知道每个喷头的位置(离草坪中心线左端的距离),以及它能覆盖到的浇灌范围。

请问:如果要同时浇灌整块草坪,最少需要打开多少个喷头?

思路

首先直径不足 \(w\) 直接排除。

然后我们希望每个在覆盖到左边的情况下越右越好。

所以我们按左端点排序后贪心取右端点即可。

Code

// Problem: #10002. 「一本通 1.1 例 3」喷水装置
// Contest: LibreOJ
// URL: https://loj.ac/p/10002
// Memory Limit: 512 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
//#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
//#define M
//#define mo
//#define N
struct node
{
	double l, r; 
}a[15010]; 
int n, m, i, j, t; 
double w, l, x, k; 
double nwmxrght, mnlft; 
int ans; 

bool cmp(node x, node y)
{
	return x.l<y.l; 
}

void work()
{
	// if(a[n].r<l) 
	// {
		// printf("-1\n"); 
		// return ; 
	// }
	i=1; 
	mnlft=0; nwmxrght=0; ans=0; 
	while(nwmxrght<l)
	{
		nwmxrght=mnlft; 
		if(a[i].l>mnlft) 
		{
			printf("-1\n"); 
			return ; 
		}
		for(; a[i].l<mnlft&&i<=n; ++i)
			nwmxrght=max(nwmxrght, a[i].r); 
		mnlft=nwmxrght; 
		++ans; 
	}
	printf("%d\n", ans); 
	return ; 
}

signed main()
{
//	freopen("tiaoshi.in", "r", stdin); 
//	freopen("tiaoshi.out", "w", stdout); 
	t=read(); 
	while(t--)
	{
		n=read();
		scanf("%lf%lf", &l, &w); 
		m=0; 
		for(i=1; i<=n; ++i)
		{
			scanf("%lf%lf", &x, &k); 
			if(k<=w/2) continue; 
			a[++m].l=x-sqrt(k*k-w*w/4); 
			a[m].r=x+sqrt(k*k-w*w/4); 
		}
		n=m; 
		sort(a+1, a+n+1, cmp); 
		work(); 
	}
	return 0; 
}

posted @ 2021-12-04 21:51  zhangtingxi  阅读(72)  评论(0编辑  收藏  举报