題目
題意
一條l長的路上栽了n個(gè)路燈捆愁,給出每個(gè)路燈的位置割去,問能覆蓋整條路的最小的路燈照射范圍。
代碼
#include<bits/stdc++.h>
using namespace std;
int main() {
int n,l;
double s[1001]={0};
scanf("%d%d",&n,&l);
for(int i=1; i<=n; i++) {
scanf("%lf",&s[i]);
}
sort(s,s+n+1);
s[n+1]=l;
double tmp,max=-1.0;
for(int i=0; i<=n; i++) {
if(i==0) {//記錄最左側(cè)的路燈的最小照射范圍
tmp=s[i+1]-s[i];
} else if(i==n) {//若最右側(cè)的路燈最小照射范圍大于最左側(cè)的昼丑,則更新tmp
double t=s[i+1]-s[i];
tmp=tmp<t?t:tmp;
} else {//找到最大的范圍呻逆,可以讓任意兩路燈互相照射到
double p=(s[i+1]-s[i])/2.0;//注意除2.0
if(p>max) {
max=p;
}
}
}
printf("%.10lf\n",tmp<max?max:tmp);//保證最大照射范圍能覆蓋兩頭的照射范圍
return 0;
}