CodeRazer's blog

By CodeRazer, history, 20 months ago,

The following code gives the correct result on my machine, but doesn't give the right answer on codeforces. 125707404 any help will be be appreciated

#include <stdio.h>
#include <stdlib.h>

int comparator (const void * p1, const void * p2)
{
return (*(double*)p1 - *(double*)p2);
}

double maxx(double a, double b) {
if (a > b){
return a;
}
else{
return b;
}
}

int main() {
int n;
double l;
scanf("%d %lf", &n, &l);
double a[n+1];
for (int i = 0; i<n; ++i) {
scanf("%lf", &a[i]);
}
qsort(a, n, sizeof(double), comparator);
double maxdiff = 0;
double prev = a[0];
maxdiff = (a[0] - 0)*2;
for (int i = 1; i<n; ++i){
maxdiff = maxx(maxdiff, a[i] - prev);
prev = a[i];
}
maxdiff = maxx(maxdiff, l*2 - prev*2);
double ans;
ans = maxdiff/2;

printf("%lf\n", ans);
return 0;
}

• +10

| Write comment?
 » 20 months ago, # |   +8 I couldn't find any issue with the code, as it seemed to work as intended on my machine as well.I used some printf statements to check the values of the variables after the input had been taken, and ran it on the codeforces custom invocation tab. What i observed was that the input worked fine for the G++ and clang compilers, but for "GNU GCC C11 5.1.0", there were some issues with taking inputs of type double (The printf statements showed that the entire array $a$, and the variable $l$, all of type double, were = $0$, after the input had been taken). Interestingly, there were no issues taking inputs of type float (but the problem required a greater degree of precision, so double had to be used by any means). Although I might not be correct, it might be a compiler specific issue on codeforces. Also, when I submitted the same code using the g++17 compiler, your answer was correct and got accepted :)