路灯 #暴力题

题目描述
一条长l的笔直的街道上有n个路灯,若这条街的起点为0,终点为l,第i个路灯坐标为ai ,每盏灯可以覆盖到的最远距离为d,为了照明需求,所有灯的灯光必须覆盖整条街,但是为了省电,要使这个d最小,请找到这个最小的d。
输入描述:
每组数据第一行两个整数n和l(n大于0小于等于1000,l小于等于1000000000大于0)。第二行有n个整数(均大于等于0小于等于l),为每盏灯的坐标,多个路灯可以在同一点。
输出描述:
输出答案,保留两位小数。
示例1
输入
复制
7 15
15 5 3 7 9 14 0
输出
复制
2.50


#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int main() {
    int n,l;
    vector<double> pos;
    while (cin>>n>>l)  {
        for (int i=0;i<n;++i) {
            double k; scanf("%lf", &k);
            pos.push_back(k);
        }
        sort(pos.begin() ,pos.end());
        double res=max(pos[0],  l-pos[n-1]);
        
        for (int i=1;i<n;++i) {
            double x = pos[i]-pos[i-1];
            if (x/2 > res) res = x/2;
            
        }
        
        printf("%.2lf
",res);
        //记得清楚数据,被坑了一次
        pos.clear();
        
        
        
    }
}


原文地址:https://www.cnblogs.com/lyr-2000/p/14259559.html