喷水装置二
先把每个喷水装置所能喷到的范围算出来(同时跳过不符合要求的),然后按照左边界排序,将一些边界超过0和W的分别归为0和W(方便下一步操作),如果第一个范围的左边界大于0(即覆盖不到0),则不能完成覆盖,输出0,如果最大的右边界小于W,也不能完全覆盖,输出0,然后从第一个开始找能覆盖到0的最大的右边界的那个区间,找到之后count++,重新从上个找到的最大右边界开始往后找,直到找到的最大右边界为W结束,中间如果找到的最大右边界等于原先的边界则无解!
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
struct SS
{
int a;
int b;
}s[10002];
bool cmp(struct SS p1,struct SS p2)
{
if(p1.a<p2.a)
return true;
if(p1.a==p2.a && p1.b<=p2.b)
return true;
return false;
}
int main()
{
int t,n,w,h,i,k,x,r,count,max;
cin>>t;
while(t--)
{
cin>>n>>w>>h;
k=0;
max=-1;
for(i=0;i<n;i++)
{
cin>>x>>r;
if(r>h/2)
{
s[k].a=x-sqrt((double)r*r-(double)(h/2)*(h/2));
s[k].b=x+sqrt((double)r*r-(double)(h/2)*(h/2));
if(s[k].a<0) //预处理去掉无效的部分,【0,w】之外的
s[k].a=0;
if(s[k].b>w)
s[k].b=w;
if(s[k].b>max)
max=s[k].b;
k++;
}
}
sort(s,s+k,cmp);
if(s[0].a!=0||max!=w)//这种情况无解
{
cout<<"0"<<endl;
continue;
}
count=0;
int f=0;
int start=0,last=0;
while(start!=w)
{
for(i=f; i<k; i++)
if(s [i].a<=start&&s[i].b>last)
{
last=s[i].b;
f=i+1;
}
if(last==start)//这种情况也无解
{
count=0;
break;
}
start=last;//从新定义边界
count++;
}
cout<<count<<endl;
}
return 0;
}