Need help with codeforces "Vanya and lanterns"

So here’s the link to the question. Problem - B - Codeforces

Here is my solution: Submission #74751893 - Codeforces

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define debug(var) cout << #var << ": " << var << "\n"
#define SORTV(vec) sort((vec).begin(), (vec).end())

void solve() {
    int n, l; cin >> n >> l;  // number of lanterns and length of road
    vector<int> lights(n);
    for (int i = 0; i < n; ++i) {
        cin >> lights[i];
    }
    SORTV(lights);
    int maxDist = 2*max(lights[0], l-lights[n-1]);
    for (int i = 0; i < n-1; ++i) {
        maxDist = max(maxDist, lights[i+1]-lights[i]);
    }
    cout << maxDist/(double)2;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    solve();
}

Here’s the solution given in the editorial

#include <stdio.h>
#include <algorithm>
using namespace std;
int n,i,a[100500],rez,l;
int main()
{
    scanf("%d%d",&n,&l);
    for (i = 0; i < n; i++)
        scanf("%d",&a[i]);
    sort(a,a+n);
    rez = 2*max(a[0],l-a[n-1]);
    for (i = 0; i < n-1; i++)
        rez = max(rez, a[i+1]-a[i]);
    printf("%.10f\n",rez/2.);
    return 0;
}

Although my solution follows the exact same logic it’s failing test case 3. (You can see the test case in the link to my submission after clicking "Click here to see test details at the bottom of the page)

Any idea what I’m doing wrong?

Add

cout<<fixed<<setprecision(10);

Works like a charm! So in cpp do doubles and floats get rounded? If so then how did it pass test 1 since it that test also has the answer as X.5000… ?

It doesn’t get rounded, but displays it in scientific notation for large values, unless explicitly specified to output in decimal notation.

1 Like

The more you know…
Thanks for suck a quick reply!