1004: LAMP
Time Limit: 1 Sec Memory Limit: 128 MB 64bit IO Format: %lld
Submitted: 176 Accepted: 62
[Submit][Status][Web Board]
Description
Pang Pang is absored in Linux now, he enjoys using LAMP(Linux + Apache + MySQL + php), and runs a great blog on it! One day, the chief of the Pang Pang’s hometown found him by his homepage, and send a email to him.
The chief asked him to solve the problem of lamp(not LAMP): The main road of the country has a lot of street lamps, which make the road beautiful at night. But at the same time, the cheif noticed that the light of the lamp overlap at many places, which will cost lots of electricity. However when the chief tried to turn down the lamps, some place were not lit. So he asked Pang to alter the brightness of all the lamps, to make sure that everywhere of the street could be lit up, and minimize the distance of each lamp could light up.
Input
Multiply test cases, terminate with EOF. The descriptions of the test cases follow:
The first line of each test case contains integers N and M (0 < N < 10000 < M < 32768). We assume that the length of the main road is M meters, starts from point 0 and ends at point M. There are N lamps on the road.
The following line contains N integers seperated by single space - the distance between the start point and the i-th street lamp.
Output
For each test case, output one line with a single real number, rounded to two decimal places.
Sample Input 7 15
14 5 2 7 9 14 1
5 20
1 7 12 18 4
Sample Output
2.50
3.00
问题描述
有一条路长M,有N个灯,问每个路灯最少需要多远才能照亮整条路?
解答:
二分法,或者结果设所有灯排序后,第一盏灯和起点的距离ds,最后一盏灯和终点的距离de,连续的两个灯之间最大的距离gap.答案就是min{ds,de,gap/2};
AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
using namespace std;
int a[10005],max1,n,m;
double ans;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(a,0,sizeof(a));
for (int i=0;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n);
max1=0;
for (int i=1;i<n;i++) max1=max(max1,a[i]-a[i-1]);
ans=max1*1.0/2;
if (ans<a[0]*1.0) ans=a[0]*1.0;
if (ans<(m-a[n-1])*1.0) ans=(m-a[n-1])*1.0;
printf("%.2lf\n",ans);
}
return 0;
}