1 / 3
Jun 2015

Dear community,

Im trying to get the time spent on input/output in my solutions to a reasonable minimum, and [url]http://www.spoj.com/problems/INOUTEST/[/url] is a great help with this. So far using _unlocked versions library functions I was able to get some speedup, but now Im stuck and would like ot ask for some help.

Basically, this version is AC in INOUTEST:

int main(int argc, char * argv[])
{
    char InputBuffer[64];
    char OutputBuffer[64];
    char * pNumCases = fgets_unlocked(InputBuffer, 64, stdin);
int numCases = atoi_spoj(pNumCases);

char * pNextCase;
char * pNextRes;
int firstNumber;
int secondNumber;
int res;

while (numCases--)
{
    pNextCase = fgets_unlocked(InputBuffer, 64, stdin);
    firstNumber = atoi_spoj(pNextCase);
    ++pNextCase;
    secondNumber = atoi_spoj(pNextCase);
    res = firstNumber * secondNumber;
    pNextRes = OutputBuffer;
    i32toa_spoj(res, pNextRes);
    fputs_unlocked(OutputBuffer, stdout);
    if (numCases)
        putchar_unlocked('\n');
}

return 0;
}

and this version fails with WA:

int main(int argc, char * argv[])
{
    char smallInputBuffer[128];
    char * pNumCases = fgets_unlocked(smallInputBuffer, 64, stdin);
int numCases = atoi_spoj(pNumCases);

const size_t input_size = numCases * 160 + 100;
const size_t output_size = numCases * 150 + 100;
char * InputBuffer = new char[input_size];
char * OutputBuffer = new char[output_size];

fread_unlocked(InputBuffer, 1, input_size, stdin);

char * pNextCase;
char * pNextRes;
int firstNumber;
int secondNumber;
int res;

pNextCase = InputBuffer;
pNextRes = OutputBuffer;

while (numCases--)
{
    firstNumber = atoi_spoj(pNextCase);
    ++pNextCase;
    secondNumber = atoi_spoj(pNextCase);
    ++pNextCase;
    res = firstNumber * secondNumber;
    i32toa_spoj(res, pNextRes);
    *pNextRes++ = '\n';
}

fwrite_unlocked(OutputBuffer, 1, pNextRes - OutputBuffer, stdout);
fflush_unlocked(0);

delete[] InputBuffer;
delete[] OutputBuffer;

return 0;
}

The magic numbers in the code should be fine for INOUTEST. I have prepared some simple test code that generates input and both version seem to produce the binary identical output on my system ( linux ).

Any ideas on why the second version is WA are welcome.

Thanks,
Ilya.

  • created

    Jun '15
  • last reply

    Jun '15
  • 2

    replies

  • 752

    views

  • 2

    users

The issue probably stems from the rest of the code that you haven't posted here.

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 14 6d

Want to read more? Browse other topics in C and C++ or view latest topics.