I tried to submit my code with GNU C++ Compiler for 126A, then I got Wrong Answer on test #31 sadly.
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<string>
using namespace std;
const double eps=1e-8;
int main()
{
long long t1,t2,t0,x1,x2;
cin>>t1>>t2>>x1>>x2>>t0;
if (t1==t2)
{
printf("%I64d %I64d\n",x1,x2);
return 0;
}
else if (t1==t0)
{
printf("%I64d 0\n",x1);
return 0;
}
else if (t2==t0)
{
printf("0 %I64d\n",x2);
return 0;
}
double t=10000000;
long long a1,a2,sum=0;
for (long long i=0;i<=(long long)x2;i++)
{
long long y1=((t2-t0)*i)/(t0-t1);
for (long long yy=min(x1,max(0ll,y1-10));yy<=max(min(x1,y1+10),0ll);yy++)
{
double tt1=(double)(t1*yy+t2*i)/(double)(yy+i);
if (tt1+eps>=(double)t0 && (fabs((double)t0-tt1)<fabs(t-(double)t0) || (fabs((double)t0-tt1)==fabs(t-(double)t0) &&
yy+i>a1+a2)))
{
a1=yy;a2=i;
t=tt1;
}
}
}
printf("%I64d %I64d\n",a1,a2);
}
But, strangely, I can pass the test #31 in my own IDE with GNU C++ Compiler just modify a little bit.
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<string>
using namespace std;
const double eps=1e-8;
int main()
{
long long t1,t2,t0,x1,x2;
cin>>t1>>t2>>x1>>x2>>t0;
if (t1==t2)
{
printf("%I64d %I64d\n",x1,x2);
return 0;
}
else if (t1==t0)
{
printf("%I64d 0\n",x1);
return 0;
}
else if (t2==t0)
{
printf("0 %I64d\n",x2);
return 0;
}
double t=10000000;
long long a1,a2,sum=0;
for (long long i=0;i<=(long long)x2;i++)
{
long long y1=((t2-t0)*i)/(t0-t1);
for (long long yy=min(x1,max(0ll,y1-10));yy<=max(min(x1,y1+10),0ll);yy++)
{
double tt1=(double)(t1*yy+t2*i)/(double)(yy+i);
if (yy==4953 && i==4533) printf("%.10f\n",tt1);
if (tt1+eps>=(double)t0 && (fabs((double)t0-tt1)<fabs(t-(double)t0) || (fabs((double)t0-tt1)==fabs(t-(double)t0) &&
yy+i>a1+a2)))
{
a1=yy;a2=i;
t=tt1;
}
}
}
printf("%I64d %I64d\n",a1,a2);
}
Then, I tried to disabled the optimizations of speed in the compiler. Surprisingly, I can also pass the test #31 with my first version.
Knowing that the optimizations of speed is enabled in Codeforces for GNU C++, I submitted my first version with MS C++ and got Accepted finally.
Why?