Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.0 kB
10
Indexable
Never
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int findMinRadius(int N, int L, vector<int>& positions) {
    sort(positions.begin(), positions.end());

    // Find the maximum gap between soldiers.
    int max_gap = 0;
    for (int i = 1; i < N; i++) {
        max_gap = max(max_gap, positions[i] - positions[i - 1]);
    }

    // Determine the radius required to cover the max gap.
    double radius = (double)max_gap / 2;

    // Special consideration for the edges.
    // Check the distance from the first soldier to the start of the road.
    radius = max(radius, (double)positions[0]);

    // Check the distance from the last soldier to the end of the road.
    radius = max(radius, (double)(L - positions[N - 1]));

    return (int)ceil(radius);
}

int main() {
    int N, L;
    cin >> N >> L;

    vector<int> positions(N);
    for (int i = 0; i < N; i++) {
        cin >> positions[i];
    }

    cout << findMinRadius(N, L, positions) << endl;
    return 0;
}