每组数据的第一行是整数n、L和W的值,其中n≤10 000。
接下来的n行,每行包含两个整数,给出一个喷头的位置和浇灌半径。
如图1所示的示意图是样例输入的第一组数据所描述的情况。
ttt.jpg
图1
输出格式:
对每组测试数据输出一个数字,表示要浇灌整块草坪所需喷头数目的最小值。如果所有喷头都打开还不能浇灌整块草坪,则输出-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。
作者
李廷元
单位
中国民用航空飞行学院
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
#include<iostream>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<cmath>
#include<vector>
#include<stack>
using namespace std;
int main()
{
int T;
cin>>T;
vector< pair<double,double> > sh;
pair<double,double> p;
while(T--)
{
int n,L;double W;
cin>>n>>L>>W;
sh.clear();
for(int i=1;i<=n;i++)
{
int x;double r;
cin>>x>>r;
if(r<W/2) continue;
r=sqrt(r*r-(W/2)*(W/2));
p=make_pair(x-r,x+r);
sh.push_back(p);
}
sort(sh.begin(),sh.end());
int i=0,ans=0;
double t=0;
bool flag=1;
while(i<sh.size())
{
if(t<sh[i].first) {flag=0;break;}
if(t>=L) break;
double max_r=-1;
while(i<sh.size()&&t>=sh[i].first)
{
max_r=max(max_r,sh[i].second);
i++;
}
t=max_r;
ans++;
}
if(flag) cout<<ans<<endl;
else cout<<"-1"<<endl;
}
}