Description
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?
Input
The first line contains two integers n, l (1 ≤ n ≤ 1000, 1 ≤ l ≤ 109) — the number of lanterns and the length of the street respectively.
The next line contains n integers ai (0 ≤ ai ≤ l). Multiple lanterns can be located at the same point. The lanterns may be located at the ends of the 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.
Sample Input
7 15 15 5 3 7 9 14 0
2.5000000000
2 5 2 5
2.0000000000
题意:n个路灯,分布在L长的街上,整点坐标。计算路灯最小的照明范围,使整条街都能有光。
解题思路:先把路灯位置排序(快速排序算法),然后计算相邻路灯距离的一半的最大值,第一个路灯与起始点的距离,最后一个路灯与尽头之间的距离,三者取最大值。
#include <stdio.h> #include<string.h> int b[1005]; void fast_sort(int a[],int low,int high) { int i=low; int j=high; int t=a[i]; if(low<high) { while(i<j) { while((a[j]>=t)&&i<j) j--; a[i]=a[j]; while((a[i]<=t)&&i<j) i++; a[j]=a[i]; } a[i]=t; fast_sort(a,low,i-1); fast_sort(a,j+1,high); } else { return; } } int main() { int n,l; int i; double d; scanf("%d%d",&n,&l); for(i=0;i<n;i++) { scanf("%d",&b[i]); } fast_sort(b,0,n-1); d=b[0]>(l-b[n-1])?b[0]:(l-b[n-1]); for( i=1;i<n;i++) { d=d>((b[i]-b[i-1])/2.0)?d:((b[i]-b[i-1])/2.0); } printf("%.10lf\n",d); return 0; }