喷水装置(算法提升篇贪心算法,课本和一本通上能找到,洛谷上没找到)
以下是零分代码(仅仅过了样例)
#include<bits/stdc++.h>
using namespace std;
int i,n,T,num;
double l,w,p,R;
struct PP{ double st,en; }pt[15055];
bool cmp(PP x,PP y){ return x.st<y.st; }
int main()
{
cin>>T;
while(T--)
{
scanf("%d%lf%lf",&n,&l,&w);
w/=2;
for(i=1;i<=n;i++)
{
scanf("%lf%lf",&p,&R);
if(R<=w/2) { n--; continue; }
//去除半径过小的无效喷头
double dis=sqrt( (R*R)-(w*w) );
pt[i].st = p-dis;
pt[i].en = p+dis;
}
sort(pt+1,pt+n+1,cmp);
p=0;//p改记已覆盖到的最右端
i=1;//i作为查询序号,只遍历一次
num=0;//已使用的喷头数
while(p<l)
{
num++;
double m=p;//记住原来的p
for(; pt[i].st<m && i<=n; i++)//从上一个i值开始找
if(p<pt[i].en) p=pt[i].en;//找到能覆盖p的最大右端
if(p==m){
printf("-1\n");
p=-1; break;//无解判断
}
}
if(p>=0) printf("%d\n",num);
}
}