1424:【例题3】喷水装置
时间限制: 1000 ms 内存限制: 65536 KB
提交数: 3003 通过数: 617
【题目描述】
长 L 米,宽 W 米的草坪里装有 n 个浇灌喷头。每个喷头都装在草坪中心线上(离两边各 W2 米)。我们知道每个喷头的位置(离草坪中心线左端的距离),以及它能覆盖到的浇灌范围。
请问:如果要同时浇灌整块草坪,最少需要打开多少个喷头?
【输入】
输入包含若干组测试数据。
第一行一个整数 T 表示数据组数;
每组数据的第一行是整数 n、L 和 W;
接下来的 n 行,每行包含两个整数,给出一个喷头的位置和浇灌半径(上面的示意图是样例输入第一组数据所描述的情况)。
【输出】
对每组测试数据输出一个数字,表示要浇灌整块草坪所需喷头数目的最小值。如果所有喷头都打开也不能浇灌整块草坪,则输出 −1 。
【输入样例】
3
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
【输出样例】
6
2
-1
【提示】
数据范围:
对于 100% 的数据,n≤15000。
问题思路:用时最少的区间个数填满整个区域
#include <bits/stdc++.h>
#define ll long long
#define PII pair<int,int>
using namespace std;
const int N=1e4+5005;
struct node
{
double l,r;
bool operator<(const node &x)const
{
return l<x.l;
}
}a[N];
int T,n;
double L,W,pos,r;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> T;
while(T--)
{
cin >> n >> L >> W;
int num=0;
while(n--)
{
cin >> pos >> r;
if(r<=W/2)
continue;
a[num].l=pos-sqrt(r*r-(W/2)*(W/2));
a[num++].r=pos+sqrt(r*r-(W/2)*(W/2));
}
sort(a,a+num);
double s=0;//s表示目前覆盖区域
int i=0,res=0;
while(s<L)
{
res++;
double t=s;
for(;a[i].l<=t&&i<num;i++)
if(a[i].r>s)
s=a[i].r;//寻找最大值
if(t==s)//不变说明中断了
{
cout << -1 << endl;
break;
}
}
if(s>=L)
cout << res << endl;
}
return 0;
}