题目描述如下:
Vanya walks late at night along a straight street of length l, lit by n lanterns. Consider the coordinate system with the beginning of the street corresponding to the point 0, and its end corresponding to the point l. Then the i-th lantern is at the point ai. The lantern lights all points of the street that are at the distance of at most d from it, where d is some positive number, common for all lanterns.
Vanya wonders: what is the minimum light radius d should the lanterns have to light the whole street?
Output:
Print the minimum light radius d, needed to light the whole street. The answer will be considered correct if its absolute or relative error doesn't exceed 10 - 9.
Hint:
Consider the second sample. At d = 2 the first lantern will light the segment [0, 4] of the street, and the second lantern will light segment[3, 5]. Thus, the whole street will be lit。
代码及分析:
1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<algorithm>
5 using namespace std;
6 const int m=1005;
7 int point[m],len[m];
8
9 int main()
10 {
11 int n,l,i;
12 double r;
13 scanf("%d%d",&n,&l);
14 point[0]=0; //先设置一个point[0]=0,目的仅仅是为了求len[1],后面可以忽略point[0];
15 for(i=1;i<=n;i++) //注意:从point[1]开始输入
16 scanf("%d",&point[i]);
17 sort(point+1,point+n+1); //从point[1]到point[n]排序!!
18 //for(i=1;i<=n;i++)
19 // printf("%d /",point[i]);
20 //memset(point,0,sizeof(point));
21
22 for(i=1;i<=n;i++)
23 len[i]=point[i]-point[i-1];
24
25 sort(len+1,len+n+1);
26 //for(i=1;i<=n;i++)
27 // printf("%d ",len[i]);
28 double maxn=len[n];
29 r=maxn/2.0;
30 //printf("%llf
",r);
31 double back=l-point[n]; //设置front记录第一个灯笼和街道首的距离
32 double front=point[1]-0; //设置back记录最后一个灯笼和街道尾的距离
33 if(r<front||r<back) // 特别注意此处分类
34 {
35 double ans;
36 ans=front>back?front:back;
37 printf("%.10llf",ans);
38 }
39 else
40 printf("%.10llf",r);
41 return 0;
42
43 }